Android 使用RelativeLayout均匀分割布局

Android 使用RelativeLayout均匀分割布局,android,android-layout,listview,android-listview,Android,Android Layout,Listview,Android Listview,我有一个Listview,其中每个Listview项都是这样的 ListViewItem是一个RelativeLayout。现在我在创建两个分屏按钮时遇到问题。现在我是这样做的 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" andr

我有一个Listview,其中每个Listview项都是这样的

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>

这是非常好的工作,但报告说

此外,嵌套使用 布局权重参数对于每个孩子来说可能特别昂贵 需要测量两次。这在以下情况下尤为重要: 布局会反复膨胀,例如在ListView或中使用时 GridView

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


<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>

对于像您的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>

android:text=“Button”/>

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

  • 首先,当您需要动态的东西时,不要使用太多的XML代码
  • 不要在XML中创建两个相对布局,而是创建一个扩展线性布局/相对布局的类
  • 将要在列表项中显示的视图添加到上述布局中
  • 在同一类中用动态测量高度和宽度
  • 并确保布局参数化,以便动态传递内容
  • 最后,您可以使用适配器**的getview方法对创建的视图进行充气
  • 请参阅以下链接


    为了最小化布局嵌套,从而优化性能,我将编写一个布局(利用布局的相关性),如下所示:

    <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>
    
    
    

    我放置了一个与中心对齐的虚拟视图,然后放置了两个与左右对齐的按钮。

    LinearLayout的
    权重非常理想,但如果您想使用RelativeLayout,则需要以百分比的形式编程设置布局的布局参数。这是一个很酷的技巧。真的帮了我的忙。谢谢分享!简单亲切的想法