问题描述
我一直在为一个项目测试一些 Presentation API,但遇到了一个问题,即正在使用的布局不是预期的布局。
我们将使用几种不同类型的外部显示器,我希望应用选择正确的布局文件。手机真的想呈现标准布局文件 - 在这台电视上的演示模式与在手机上的外观相比,它看起来很糟糕。
我的测试代码:
主要活动:
//display manager for external display
displayManager = (displayManager)getSystemService(Context.disPLAY_SERVICE);
if (displayManager!= null){
presentationdisplays = displayManager.getdisplays(displayManager.disPLAY_CATEGORY_PRESENTATION);
display[] displays = displayManager.getdisplays();
if (presentationdisplays.length > 0){
Secondarydisplay secondarydisplay = new Secondarydisplay(MainActivity.this,presentationdisplays[0]);
secondarydisplay.show();
}
}
Secondarydisplay.java
public class Secondarydisplay extends Presentation {
/**
* Creates a new presentation that is attached to the specified display
* using the default theme.
*
* @param outerContext The context of the application that is showing the presentation.
* The presentation will create its own context (see {@link #getContext()}) based
* on this context and @R_678_4045@ion about the associated display.
* @param display The display to which the presentation should be attached.
*/
public Secondarydisplay(Context outerContext,display display) {
super(outerContext,display);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondary_display);
}
我创建了这个 secondary_display.xml,无论我做什么,它在电视上看起来都是垃圾
图像被笨拙地拉伸,字体被拉伸。一切都太高了。
我创建了一个名为 layout-port-television 的备用布局文件夹(电视处于纵向模式)
并在此文件夹中制作了布局的替代版本,希望能够使用它们,但仍然使用常规布局版本。
所以我删除了常规布局版本,只留下了电视版本,应用程序因此崩溃:
04-11 09:02:41.659 28878 28878 E AndroidRuntime:android.content.res.Resources$NotFoundException:资源 ID #0x7f0c0060 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:285) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.content.res.Resources.loadXmlResourceParser(Resources.java:2514) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.content.res.Resources.getLayout(Resources.java:1357) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.view.LayoutInflater.inflate(LayoutInflater.java:530) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.view.LayoutInflater.inflate(LayoutInflater.java:479) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:541) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.app.Dialog.setContentView(Dialog.java:858) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 com.edtest.presentationtest.Secondarydisplay.onCreate(PosSecondarydisplay.java:56) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.app.Dialog.dispatchOnCreate(Dialog.java:708) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.app.Dialog.show(Dialog.java:422) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.app.Presentation.show(Presentation.java:285) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 com.edtest.presentationtest.MainActivity$1.ondisplayAdded(MainActivity.java:145) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.hardware.display.displayManagerGlobal$displayListenerDelegate.handleMessage(displayManagerGlobal.java:1575) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.os.Handler.dispatchMessage(Handler.java:106) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.os.Looper.loop(Looper.java:246) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 android.app.ActivityThread.main(ActivityThread.java:8462) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 java.lang.reflect.Method.invoke(本机方法) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596) 04-11 09:02:41.659 28878 28878 E AndroidRuntime:在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
布局就在那里。如果我将它从那个电视文件夹移到常规布局文件夹它工作正常
这是布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Secondarydisplay">
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:padding="4dp"
android:text="This Text Should Be Centered"
android:textAppearance="?android:attr/textAppearanceMedium"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:src="@drawable/beer"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView7" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="24dp"
android:src="@drawable/logo"
app:layout_constraintBottom_toTopOf="@+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
“啤酒”图像是一个方形 png 文件 - 128x128 - 72ppi “徽标”图像为 1000 x 159 png - 72ppi
我想弄清楚如何让它们正常显示而不是拉伸
我是否需要使用演示 API 为该电视输出使用备用文件夹? 如果是这样,您知道如何最好地找出可能使用的内容以便我可以构建正确的版本吗?
谢谢!
编辑
根据@CommonsWare 的建议,我在显示检查中添加了一些调试输出并得到了一些奇怪的结果
if (displayManager!= null){
presentationdisplays = displayManager.getdisplays(displayManager.disPLAY_CATEGORY_PRESENTATION);
display[] displays = displayManager.getdisplays();
Point point = new Point();
displayMetrics displayMetrics = new displayMetrics();
for (int i=0; i<displays.length; i++) {
addToLog("disPLAY: " + displays[i].getName());
addToLog("disPLAY: " + displays[i].getdisplayId());
try {
displays[i].getRealSize(point);
addToLog("disPLAY: REAL SIZE: " + point.toString());
addToLog("disPLAY: X: " + point.x);
addToLog("disPLAY: Y: " + point.y);
displays[i].getRealMetrics(displayMetrics);
addToLog("disPLAY: DENSITY: " + displayMetrics.density);
addToLog("disPLAY: DENSITY_DPI: " + displayMetrics.densityDpi);
addToLog("disPLAY: HEIGHT PIXELS: " + displayMetrics.heightPixels);
addToLog("disPLAY: WIDTH PIXELS: " + displayMetrics.widthPixels);
addToLog("disPLAY: METRICS: " + displayMetrics.toString());
addToLog("disPLAY: ROTATION: " + displays[i].getRotation());
addToLog("disPLAY: MODE: " + displays[i].getMode().toString());
//addToLog("disPLAY: CUTOUT: " + displays[i].getCutout().toString());
Context displayContext = createdisplayContext(displays[i]);
Configuration displayConfiguration = displayContext.getResources().getConfiguration();
addToLog("disPLAY: CONfigURATION: " + displayConfiguration.toString());
} catch (Exception e) {
e.printstacktrace();
} }
if (presentationdisplays.length > 0){
Secondarydisplay secondarydisplay = new Secondarydisplay(MainActivity.this,presentationdisplays[0]);
secondarydisplay.show();
}
}
对于这个特殊的显示器,如果我将它旋转回横向并告诉显示器它处于横向模式 - 手机屏幕显示在显示屏的中心
所以显示器可能在旋转时做一些奇怪的事情,因为它仍然告诉我它是横向还是纵向。我不认为电视可以传达这一点,所以这款三星显示器可能有一些专有的方式来做到这一点。
这是我的日志输出:
PRESENTATION_TEST: disPLAY: HDMI Screen
PRESENTATION_TEST: disPLAY: 118
PRESENTATION_TEST: disPLAY: REAL SIZE: Point(1920,1080)
PRESENTATION_TEST: disPLAY: X: 1920
PRESENTATION_TEST: disPLAY: Y: 1080
PRESENTATION_TEST: disPLAY: DENSITY: 1.3312501
PRESENTATION_TEST: disPLAY: DENSITY_DPI: 213
PRESENTATION_TEST: disPLAY: HEIGHT PIXELS: 1080
PRESENTATION_TEST: disPLAY: WIDTH PIXELS: 1920
PRESENTATION_TEST: disPLAY: METRICS: displayMetrics{density=1.3312501,width=1920,height=1080,scaledDensity=1.3312501,xdpi=92.015,ydpi=91.44}
PRESENTATION_TEST: disPLAY: ROTATION: 0
PRESENTATION_TEST: disPLAY: MODE: {id=3286,fps=60.000004}
PRESENTATION_TEST: disPLAY: CONfigURATION: {1.0 310mcc260mnc [en_US] ldltr sw811dp w1442dp h811dp 213dpi xlrg long land night -touch -keyb/v/h -nav/h winConfig={ mBounds=Rect(0,0 - 1080,2400) mAppBounds=Rect(0,75 - 1080,2265) mWindowingMode=fullscreen mdisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} s.3849 desktop/d dc/d ?dcui ?dcaf bts=0 ff=0 bf=0 themeSeq=0}
所以电视/显示器导致了我的拉伸/倾斜 - 也许我不应该使用它,而应该为横向显示构建一个纵向 UI...
基于此 - 当设备不相信它旋转时,您将如何制作一个旋转的布局文件?
我不知道以编程方式旋转所有 UI 元素的任何方法。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)