gpt4 book ai didi

android - 我的应用程序因不同的屏幕尺寸而崩溃

转载 作者:行者123 更新时间:2023-12-03 17:04:06 25 4
gpt4 key购买 nike

我为“layout-normal-land-1920x1080”创建了一个 android Activity ,但每次我尝试运行它时它都会崩溃,它只适用于默认的 xml 文件,但布局会针对不同的屏幕尺寸发生变化,我希望 Activity 与对于所有不同的屏幕尺寸和尺寸,相同的 UI,如果有办法制作一个响应式的 android 应用程序? folders of layout for different screen sizes
screenshots for the previews of xml files(yellow buttons)

安卓 list :

<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens= "true"
android:anyDensity="true"
/>

默认xml代码:

<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="horizontal"
tools:context=".TicketActivity"
tools:layout_editor_absoluteY="73dp">


<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.25" />

<android.support.constraint.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.50"
/>

<android.support.constraint.Guideline
android:id="@+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.75"
/>

<Button
android:id="@+id/butt1"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
android:id="@+id/butt6"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
android:id="@+id/butt11"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
android:id="@+id/butt2"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toEndOf="@+id/butt1"
app:layout_constraintTop_toTopOf="@+id/guideline"


app:layout_constraintVertical_bias="0.49" />

<Button
android:id="@+id/butt7"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toEndOf="@+id/butt6"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
android:id="@+id/butt12"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="5dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/butt11"
app:layout_constraintTop_toTopOf="@+id/guideline3"
app:layout_constraintVertical_bias="0.619" />

<Button
android:id="@+id/butt3"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="7dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toEndOf="@+id/butt2"

app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
android:id="@+id/butt8"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="7dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toEndOf="@+id/butt7"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
android:id="@+id/butt13"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="7dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/butt12"
app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
android:id="@+id/butt4"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toEndOf="@+id/butt3"
app:layout_constraintTop_toTopOf="@+id/guideline" />

<Button
android:id="@+id/butt9"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toEndOf="@+id/butt8"
app:layout_constraintTop_toTopOf="@+id/guideline2" />

<Button
android:id="@+id/butt14"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:background="#ffff0f"

android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/butt13"
app:layout_constraintTop_toTopOf="@+id/guideline3" />

<Button
android:id="@+id/butt5"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"

app:layout_constraintBottom_toTopOf="@+id/guideline2"
app:layout_constraintStart_toEndOf="@+id/butt4"
app:layout_constraintTop_toTopOf="@+id/guideline"
android:background="#ffff0f"
android:textSize="15sp"
android:textStyle="bold"

/>

<Button
android:id="@+id/butt10"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="@+id/guideline3"
app:layout_constraintStart_toEndOf="@+id/butt9"
app:layout_constraintTop_toTopOf="@+id/guideline2"
android:background="#ffff0f"
android:textSize="15sp"
android:textStyle="bold"
/>

<Button
android:id="@+id/butt15"
android:layout_width="110dp"
android:layout_height="63dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/butt14"
app:layout_constraintTop_toTopOf="@+id/guideline3"
android:background="#ffff0f"
android:textSize="15sp"
android:textStyle="bold"
/>

<TextView
android:id="@+id/themeTxt"
android:layout_width="wrap_content"
android:layout_height="41dp"
android:layout_marginTop="15dp"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textSize="15pt"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>

错误:
Process: com.example.asad.tdk, PID: 3667
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.asad.tdk/com.example.asad.tdk.TicketActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f09001e
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f09001e
at android.content.res.ResourcesImpl.getValue(ResourcesImpl.java:190)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2101)
at android.content.res.Resources.getLayout(Resources.java:1115)
at android.view.LayoutInflater.inflate(LayoutInflater.java:424)
at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
at com.example.asad.tdk.TicketActivity.onCreate(TicketActivity.java:76)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6119) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

最佳答案

要针对不同的屏幕分辨率维护您的应用程序,您可能必须使用多个布局或多个 dimens.xml 文件。
基本上,我们维护布局
340 X 480 - mdpi
540 X 960 - hdpi
720 X 1280- Xhdpi
1080 X 1920- XXhdpi(1080 X 2160,1080 X 2200 全屏显示)
1440 X 2880- XXXhdpi
6.5 到 7.5 英寸标签 - sw600dp
7.5-10.1英寸tab-sw720dp
大于以上-sw800dp

如果您想维护多个设备的布局,请为上述分辨率名称创建 xml,即 布局-xxhdpi,布局-xxxhdpi .景观- 布局-土地-hdpi

但是android应用程序不能真正响应。你可以做其他事情,比如为 340 X 480 设计,即 mdpi。让 Android 系统使用 DP 计算来扩展它。这样,您的 UI 将对移动设备有所响应。

关于android - 我的应用程序因不同的屏幕尺寸而崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52647056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com