【问题标题】:Multiple screen sizes and XML Layout多种屏幕尺寸和 XML 布局
【发布时间】:2014-02-03 22:10:27
【问题描述】:

我已经阅读了我能找到的所有内容,但我无法弄清楚这一点。我有一个带有标题的 XML,然后是一个列表视图,最后一行有 2 个按钮。为了使布局看起来完美,我已经“硬编码”了列表视图的大小(467dp)。这在我的三星 Galaxy S4 上很好,但我不确定它在其他尺寸略有不同的手机上是否合适。我在 Galaxy 8" 选项卡上对其进行了测试,但它看起来不正确。然后我在 10.1" 选项卡上对其进行了测试,它(再次)看起来不正确。基本上底部按钮在屏幕中间。我通过为 sw600dp 和 sw720dp 创建布局来解决这个问题。对于其中的每一个,我必须为列表视图硬编码不同的大小。在我看来,有一种更好的方法可以让标题列表视图按钮 XML 在任何设备上显示(相对)相同。谁能告诉我如何更改我的 XML,这样我就不必对列表视图的大小进行硬编码?

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/title_line"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:gravity="center"
    android:text="@string/workout_locations">
</TextView>

<ListView
    android:id="@+id/location_list"
    android:layout_width="match_parent"
    android:layout_height="467dp"
    android:longClickable="true" >
</ListView>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:orientation="horizontal" >

    <ImageButton
        android:id="@+id/help_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:contentDescription="@string/help_description"
        />

    <ImageButton
        android:id="@+id/add_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:contentDescription="@string/add_description"
        />
</LinearLayout>

</LinearLayout>

【问题讨论】:

    标签: android xml listview android-listview


    【解决方案1】:

    android:layout_weight="1 在按钮中添加这个

    【讨论】:

      【解决方案2】:

      我想知道这是否有帮助:

      确保整个布局是一个relativeLayout,并在里面放listview

      <ListView
          android:id="@+id/location_list"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="50dp" // the size of the buttons height
          android:longClickable="true" >
      </ListView>
      

      在其下方是另一个带有按钮的相对布局。

      <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="50dp"
      android:layout_gravity="bottom" 
      android:layout_alignParentBottom="true">
      
      <ImageButton
          android:id="@+id/help_button"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:contentDescription="@string/help_description"
          />
      
      <ImageButton
          android:id="@+id/add_button"
          android:layout_width="wrap_content"
          android:layout_height="match_parent"
          android:contentDescription="@string/add_description"
          />
      

      如果这仍然导致问题,那么你可以写:

      android:layout_above="@+id/relButtonLayout"
      

      在列表视图中。

      【讨论】:

      • 这个对我有用。我把所有东西都放在一个相对布局中(不需要多个相对布局),将最后一个列表视图(带有按钮的那个)设置为“android:layout_alignParentBottom="true",一切都很好。我对所有其他 XML 都做了同样的事情文件在我的项目中,它看起来很棒。谢谢!
      • 很高兴它有帮助:)
      【解决方案3】:

      使用 layout_weight 占据尽可能多的空间。

      <ListView android:id="@+id/location_list"
              android:layout_width="match_parent"
              android:layout_height="0dip"
              android:layout_weight="1"
          android:longClickable="true" >
      </ListView>
      

      【讨论】:

      • 见下文,这不起作用,因为按钮向上滚动到屏幕顶部。我需要它们停留在屏幕的最底部。
      【解决方案4】:

      如果您对某些尺寸进行了硬编码,您可以打赌它在大多数设备中看起来都不会很好。为此,最好将layout_heightlayout_weight 设置为wrap_contentmatch_parent,具体取决于您的需要。

      对于您描述的案例,还有另一个重要工具:layout_weight,您可能已经使用过。第一次使用ListView 来适应您想要的设计可能会很困难,但是一旦您发现如何设置其布局,其他人就很容易了。

      在我的情况下,这个定义总是可以正常工作:

      <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
      
        <ListView
          android:id="@+id/mylistview"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1" 
          android:scrollbars="vertical" >
        </ListView>
      </LinearLayout>
      

      看一下:我设置了一个单数 LinearLayout(在这种情况下,因为它的视图比我展示的 ListView 更多),但我正在设置 @987654330 的权重@ 到 1,即 LinearLayout 1 中的 sumWeights。这样您就可以向自己保证 ListView 将尽可能地扩展,而无需硬编码值。

      这只是玩一会儿的问题,但是硬编码的值越少,在其他设备上的适应性就越好。

      【讨论】:

      • 我已经试过了。问题是按钮需要留在底部。当您将 ListView 定义为 0dp 时,按钮会向上滚动到屏幕顶部。我需要它们停留在屏幕的最底部。
      • 然后只需将您的按钮添加到另一个RelativeLayout 中,并将orientation 属性设置为horizontal 就在LinearLayout 的下方,它包含您的ListView,并将其layout_alignParentBottom 设置为@987654340 @.
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-20
      • 1970-01-01
      相关资源
      最近更新 更多