Android如何使按钮适合各种屏幕尺寸

时间:2013-05-24 14:10:23

标签: android layout view screen

我的应用程序有大约10个按钮的菜单。在我的测试手机(HTC ONE X)上,所有按钮都正确显示。在我的另一部测试手机(HTC Desire C)屏幕上有几个按钮。缺少底部的2个按钮和textview。

如何使整个布局适合各种屏幕尺寸?我是否必须在运行时获取屏幕大小,例如使用getDisplayMetrics等?

先谢谢,马特。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/carefreebgscaledlighting"
    android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android">


    <TextView
        android:id="@+id/textviewcompanyname"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

    <TextView
        android:id="@+id/spacerasnexttextviewclasheswithbg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge"
         />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/stringloggedinscreen"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#003F87" />

<Button
    android:id="@+id/buttonsignin"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringbuttonsignin" />
<Button
    android:id="@+id/buttongetrota"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringbuttongetrota" />


<Button
    android:id="@+id/buttongetphonenumbers"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringbuttongetphonenumbers" />


    <Button
        android:id="@+id/viewtransactionsactual"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="View Transactions" />


    <Button
        android:id="@+id/buttondeletecarertable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/stringbuttondeletetable" />

     <Button
        android:id="@+id/buttonloadtransactionsmap"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="@string/stringloadtransactionmap" />


     <Button
         android:id="@+id/buttonloneworker"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:text="@string/buttonloneworker" /> 


      <Button
         android:id="@+id/buttonsendOutstandingTransactions"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:text="@string/stringSendOutstandingTransactions" /> 

      <Button
         android:id="@+id/buttoncreatemanuallogout"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:layout_gravity="center"
         android:text="@string/stringbuttoncreatemanuallogout" />



     <RelativeLayout
         xmlns:android="http://schemas.android.com/apk/res/android"
         android:layout_width="fill_parent"
         android:layout_height="fill_parent" >

         <TextView
        android:id="@+id/textViewYouAreSignedIn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_alignParentTop="true"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
         />

          <TextView
        android:id="@+id/textViewUnsentTransactions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_below="@id/textViewYouAreSignedIn"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#FFFFFF"
         />

         <TextView
        android:id="@+id/textViewVersionmenuscreen"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/stringtextviewversion"

        android:layout_alignParentBottom="true"/>
     </RelativeLayout>

    </LinearLay

出&GT;

2 个答案:

答案 0 :(得分:2)

您需要针对您将使用的最小屏幕尺寸进行开发,或者您需要单独的布局,其中较小的布局较少放在一行并且包含在ScrollView中。

答案 1 :(得分:2)

为什么不将父布局放在ScrollView中,这样当屏幕太小时,用户就可以向下滚动以查看隐藏的项目。 您的布局应如下所示:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android">
>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/carefreebgscaledlighting"
android:orientation="vertical">


<TextView
    android:id="@+id/textviewcompanyname"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#003F87" />

<TextView
    android:id="@+id/spacerasnexttextviewclasheswithbg"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
     />

<TextView
    android:id="@+id/textview1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringloggedinscreen"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#003F87" />

    <Button
    android:id="@+id/buttonsignin"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringbuttonsignin" />
    <Button
    android:id="@+id/buttongetrota"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringbuttongetrota" />


   <Button
   android:id="@+id/buttongetphonenumbers"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_gravity="center"
   android:text="@string/stringbuttongetphonenumbers" />


   <Button
    android:id="@+id/viewtransactionsactual"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="View Transactions" />


  <Button
    android:id="@+id/buttondeletecarertable"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringbuttondeletetable" />

 <Button
    android:id="@+id/buttonloadtransactionsmap"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/stringloadtransactionmap" />


 <Button
     android:id="@+id/buttonloneworker"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:text="@string/buttonloneworker" /> 


  <Button
     android:id="@+id/buttonsendOutstandingTransactions"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:text="@string/stringSendOutstandingTransactions" /> 

  <Button
     android:id="@+id/buttoncreatemanuallogout"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:layout_gravity="center"
     android:text="@string/stringbuttoncreatemanuallogout" />



  <RelativeLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent" >

     <TextView
    android:id="@+id/textViewYouAreSignedIn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_alignParentTop="true"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
     />

      <TextView
    android:id="@+id/textViewUnsentTransactions"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_below="@id/textViewYouAreSignedIn"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
     />

     <TextView
    android:id="@+id/textViewVersionmenuscreen"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/stringtextviewversion"

    android:layout_alignParentBottom="true"/>
   </RelativeLayout>

  </LinearLayout>
  </ScrollView>
相关问题