调整 android activity 的布局以适合所有屏幕并始终显示所有信息

Adjusting layout of android activity to fit all screens and always display all information

我的应用程序中有上述布局,但是我使用 Note 3 作为测试设备,每当我进行设计时,它对于大多数其他 phones 来说都太大了。这是我的 XML 文件。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:background="@drawable/backrepeat"

    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"
        android:id="@+id/linearLayout">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:gravity="right"
            android:text="Wynik:    "
            android:id="@+id/textView2"
            android:layout_alignParentBottom="true"
            />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="0/0"
            android:id="@+id/score"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />



    </LinearLayout>
<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="100dp"
    android:id="@+id/firstbutton"
    android:gravity="center_horizontal"
    android:layout_below="@+id/question">
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/answera"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:shadowDx="0"
        android:shadowDy="0"
        android:shadowRadius="5"
        android:onClick="Clicking"
        android:textSize="18dp"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="23dp" />


</LinearLayout>
    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/secondbutton"
        android:gravity="center_horizontal"
        android:layout_below="@id/firstbutton">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/answerb"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
            android:textSize="18dp"
            android:onClick="Clicking"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="23dp"/>

    </LinearLayout>

   <LinearLayout
       android:layout_width="fill_parent"
       android:layout_height="100dp"
       android:id="@+id/thirdbutton"
       android:gravity="center_horizontal"
       android:layout_below="@id/secondbutton">
       <Button
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="New Button"
           android:id="@+id/answerc"
           android:background="@drawable/buttonshape"
           android:shadowColor="#A8A8A8"
           android:shadowDx="0"
           android:shadowDy="0"
           android:shadowRadius="5"
           android:textSize="18dp"
           android:onClick="Clicking"
           android:layout_centerHorizontal="true"
           android:layout_marginTop="23dp">

       </Button></LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/fourthbutton"
        android:gravity="center_horizontal"
        android:layout_below="@id/thirdbutton">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="New Button"
            android:id="@+id/answerd"
            android:background="@drawable/buttonshape"
            android:shadowColor="#A8A8A8"
            android:shadowDx="0"
            android:shadowDy="0"
            android:shadowRadius="5"
            android:onClick="Clicking"
            android:textSize="18dp"
            android:layout_above="@+id/linearLayout"
            android:layout_marginTop="23dp"
            />
    </LinearLayout>




    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:textSize = "16sp"
        android:id="@+id/question"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />




</RelativeLayout>

我希望布局适合任何 phone 大小并自动重新调整,以便第一个按钮始终位于文本下方,所有按钮均匀分布,它们之间有一个 space,并且最后一个按钮始终位于底部文本上方,并相应调整字体。我不确定如何用我的 XML.

完成此操作

更新:

Process: com.example.albert.mathapp, PID: 3832
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.albert.mathapp/com.example.albert.mathapp.MainActivity}: android.view.InflateException: Binary XML file line #54: Error inflating class Button
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access0(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #54: Error inflating class Button
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.albert.mathapp.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:5389)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access0(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1
            at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:464)
            at android.widget.TextView.<init>(TextView.java:1415)
            at android.widget.Button.<init>(Button.java:108)
            at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:60)
            at android.support.v7.widget.AppCompatButton.<init>(AppCompatButton.java:56)
            at android.support.v7.internal.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:97)
            at android.support.v7.app.AppCompatDelegateImplV7.createView(AppCompatDelegateImplV7.java:782)
            at android.support.v7.app.AppCompatDelegateImplV7.onCreateView(AppCompatDelegateImplV7.java:810)
            at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:690)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
            at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:249)
            at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:106)
            at com.example.albert.mathapp.MainActivity.onCreate(MainActivity.java:61)
            at android.app.Activity.performCreate(Activity.java:5389)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
            at android.app.ActivityThread.access0(ActivityThread.java:163)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5335)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

我建议使用权重而不是设置高度。在下面的示例中,我使用了您的示例(删除了我没有的背景)并将每个包含按钮的布局的权重设置为 1。我还设置了布局的父级以匹配父级,因此布局包含按钮将根据其父级大小调整其大小。

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true">

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="right"
            android:text="Wynik:    "
            android:textAppearance="?android:attr/textAppearanceLarge"
            />

        <TextView
            android:id="@+id/score"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:text="0/0"
            android:textAppearance="?android:attr/textAppearanceLarge"/>


    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/question"
        android:layout_marginBottom="56dp"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal">


            <Button
                android:id="@+id/answera"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="23dp"
                android:onClick="Clicking"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="5"
                android:text="New Button"
                android:textSize="18dp"/>

        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal">


            <Button
                android:id="@+id/answerb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="23dp"
                android:onClick="Clicking"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="5"
                android:text="New Button"
                android:textSize="18dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal">


            <Button
                android:id="@+id/answerc"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_marginTop="23dp"
                android:onClick="Clicking"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="5"
                android:text="New Button"
                android:textSize="18dp"/>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center_horizontal">


            <Button
                android:id="@+id/answerd"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_above="@+id/linearLayout"
                android:layout_marginTop="23dp"
                android:onClick="Clicking"
                android:shadowColor="#A8A8A8"
                android:shadowDx="0"
                android:shadowDy="0"
                android:shadowRadius="5"
                android:text="New Button"
                android:textSize="18dp"
                />
        </LinearLayout>
    </LinearLayout>


    <TextView
        android:id="@+id/question"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="New Text"
        android:textSize="16sp"/>

</RelativeLayout>