Android:如何在 RelativeLayout 中均匀分布组件?

标签 android

我正在尝试组织 3 个 LinearLayouts,这样一个向左对齐,一个向右对齐,第三个在两者之间,两边的空白量相等。我想我将它们包装在 RelativeLayout 中以完成此操作。然后将第一个布局设置为 alignParentLeft,将第二个布局设置为 alignParentRight。但是对于第三个布局,我想设置 layout_marginLeft = (RelativeLayout.width - (layout1.width + layout2.width + layout3.width))/2。虽然,我看不到如何在 xml 中执行此操作。有什么建议吗?

*编辑。根据建议,我现在使用的是 LinearLayout。这更接近于工作,但 3 个布局不是均匀分布的,正确的布局不是右对齐的。下面是我的 xml:

<LinearLayout 
        android:id="@+id/toolbar"
        android:orientation="horizontal"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_gravity="bottom"
        android:paddingTop="4dip"
        android:paddingBottom="4dip"
        android:background="#50000000"
        android:visibility="visible">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
               <ImageButton 
                    android:id="@+id/open"
                    android:src="@drawable/open"
                    android:layout_width="wrap_content" 
                    android:layout_height="wrap_content">
               </ImageButton>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
            <ImageButton 
                android:id="@+id/prev"
                android:src="@drawable/prev"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
            <TextView
                android:id="@+id/totalpages"
                android:text="0 of 0"
                android:layout_gravity="center"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content" 
                android:textColor="#FFFFFFFF">
            </TextView>
            <ImageButton 
                android:id="@+id/next"
                android:src="@drawable/next"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
        </LinearLayout>
        <LinearLayout
            android:orientation="horizontal"
            android:layout_gravity="right"
            android:layout_width="wrap_content"
            android:layout_weight="1"
            android:layout_height="wrap_content">
            <ImageButton 
                android:id="@+id/zoomout"
                android:src="@drawable/zoomout"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
            <ImageButton 
                android:id="@+id/zoomin"
                android:src="@drawable/zoomin"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content">
            </ImageButton>
        </LinearLayout>
    </LinearLayout>

最佳答案

使用 LinearLayout 而不是 RelativeLayout 并使用 layout_weight 属性。 您可能需要将 layout_width 设置为 0dip,然后将权重设置为 0.33 之类的值

编辑: 是的,您可以使用权重 1 或任何其他小于 1 的相等权重。 例子

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:text="string 1 for test...."
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
        android:layout_gravity="center_horizontal"
            android:text="string 2 for test...."
            />
    </LinearLayout>
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="0dip"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        >
        <TextView  
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
        android:layout_gravity="right"
            android:text="string 3 for test...."
            />
    </LinearLayout>
</LinearLayout>

关于Android:如何在 RelativeLayout 中均匀分布组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4338792/

相关文章:

android - 使用复选框过滤 ListView

android - 使用文件传输上传大于 15mb 的视频时 Phonegap 崩溃

java - 尝试执行databasehelper的delete方法时出现nullpointerexecption

java - Android,内部有多个 View 的滚动菜单 - 来自 XML 的 View

android - 如何在 Windows 上使用 Charles Web Proxy 和最新的 Android Emulator 配置 SSL 证书?

android - 无法使用 Realm 编译Android应用程序

android - 动态创建布局(类似于listview)

java - Android:如何告诉我的 Activity 从我的服务启动新 Activity ?

Android 将光标从一个 EditText 移动到另一个?

android - 使用 android 发出 HTTP 请求