【问题标题】:Android how to make buttons fit on various screen sizesAndroid如何使按钮适合各种屏幕尺寸
【发布时间】:2013-05-24 14:10:22
【问题描述】:

我有一个包含大约 10 个按钮的菜单的应用。在我的测试手机(HTC ONE X)上,所有按钮都正确显示。在我的另一部测试手机(HTC Desire C)上,屏幕上有几个按钮。底部的 2 个按钮和文本视图不见了。

如何使整个布局适合各种屏幕尺寸?我是否必须在运行时获取屏幕尺寸,例如使用 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

出来>

【问题讨论】:

标签: android layout view screen


【解决方案1】:

为什么不把父布局放在一个 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>

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-10-14
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 2022-01-24
      • 1970-01-01
      • 2016-07-02
      • 2016-07-02
      • 2021-06-21
      相关资源
      最近更新 更多