【问题标题】:Using a RelativeLayout to split a layout evenly使用 RelativeLayout 均匀分割布局
【发布时间】:2014-10-30 06:12:48
【问题描述】:

我有一个列表视图,其中每个列表视图项看起来像这样!

ListViewItem 是一个RelativeLayout。现在我在创建两个分屏按钮时遇到问题。目前我正在这样做。

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

    <LinearLayout 
        android:layout_height="fill_parent" android:layout_width="fill_parent"
        android:layout_below="@+id/ReviewText">

    <RelativeLayout
        android:layout_weight="1"
        android:layout_height="fill_parent" android:layout_width="0dp"
        android:onClick="likeClicked"
        android:clickable="true" >
        <!-- SOME CODE -->
    </RelativeLayout>

    <RelativeLayout
        android:layout_weight="1"
        android:layout_height="fill_parent" android:layout_width="0dp"
        android:onClick="likeClicked"
        android:clickable="true" >
        <!-- SOME CODE -->
    </RelativeLayout>

    </LinearLayout>

<RelativeLayout>

这工作得很好,但android dev documentation here

此外,嵌套了几个使用 layout_weight 参数可能特别昂贵,因为每个孩子 需要测量两次。这一点尤其重要,当 布局反复膨胀,例如在 ListView 中使用时或 网格视图。

我可以改进我的代码以提高性能吗?如果是,如何?有没有其他方法可以在不使用 LinearLayout 的情况下均匀拆分两个按钮?

【问题讨论】:

  • LinearLayout's weight 是理想的,但如果你想用 RelativeLayout 来做,你需要以编程方式设置布局的百分比参数。检查这个例如代码stackoverflow.com/a/16519595/1939564

标签: android android-layout listview android-listview


【解决方案1】:

为了最小化布局嵌套,为了优化性能,我会编写一个布局(它确实利用了布局的相对性) 喜欢这个:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <View
        android:id="@+id/dummy"
        android:layout_width="1dp"
        android:layout_height="1dp"
        android:layout_centerHorizontal="true"
        android:visibility="invisible"
    />
    <Button
        android:id="@+id/btnLeft"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@id/dummy"
        android:onClick="likeClicked"
        android:clickable="true"
    />
    <Button
        android:id="@+id/btnRite"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_toRightOf="@id/dummy"
        android:onClick="likeClicked"
        android:clickable="true"
    />
<RelativeLayout>

我放了一个与中心对齐的虚拟视图,然后放了2个按钮,我分别在它的左侧和右侧对齐。

【讨论】:

  • 这是一个很酷的技巧。真正帮助我解决了我一直在努力的布局。感谢分享!
  • 简单的好主意
【解决方案2】:

对于像您的 LinearLayout 这样的简单布局是完美的选择。唯一需要注意的是将布局权重嵌套在其父级已经分配了布局权重的视图中。这完全没问题:

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

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" />

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/button1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

虽然不是这样:

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" > <!-- nesting this way is bad for performance -->
            android:text="Button" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Button" />
        </Button>
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical" >

        <!-- this is ok -->

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>
</LinearLayout>

【讨论】:

    【解决方案3】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:orientation="vertical" 
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
    
    <place top item layout here>   
    
    <LinearLayout 
        android:layout_height="fill_parent" android:layout_width="fill_parent"
         android:orientation="horizontal" 
           android:weightSum="2">
    
    <RelativeLayout
        android:layout_height="fill_parent" android:layout_width="0dp"
        android:onClick="likeClicked"
        android:clickable="true" 
         android:layout_weight="1"
        >
        <!-- SOME CODE -->
    </RelativeLayout>
    
    <RelativeLayout
        android:layout_weight="1"
        android:layout_height="fill_parent" android:layout_width="0dp"
        android:onClick="likeClicked"
        android:clickable="true" >
        <!-- SOME CODE -->
    </RelativeLayout>
    
    </LinearLayout>
    
     </LinearLayout>
    

    【讨论】:

      【解决方案4】:

      希望,如果您遵循列出的要点,性能可以得到提高

      1. 当您需要动态的东西时,第一件事情不会使用太多的 XML 代码
      2. 不要在 XML 中创建 2 个相对布局,而是创建一个扩展线性布局/相对布局的类
      3. 将要在列表项中显示的视图添加到上述布局中
      4. 在同一类中动态测量高度和宽度
      5. 并确保布局已参数化,您可以在其中动态传递内容
      6. 最后,您可以使用适配器的 getview 方法对创建的视图进行充气**

      参考以下链接

      Dynamic listview content loader

      【讨论】:

        猜你喜欢
        • 2014-02-06
        • 1970-01-01
        • 1970-01-01
        • 2012-07-10
        • 1970-01-01
        • 1970-01-01
        • 2011-04-04
        • 2013-01-05
        • 2011-09-16
        相关资源
        最近更新 更多