iGi*_*o90 39 android android-layout android-relativelayout
我多次尝试绘制2个相对布局水平对齐并分为半屏.

我用油漆设计图像来更好地解释我的意思.
有什么建议吗?
小智 111
在不需要使用LinearLayout的情况下完成相同任务的另一种方法是在父布局的中间放置一个居中对齐的"垫片",然后将其他元素对齐.如果你将半角元素的宽度设置为match_parent,但是同时对齐它们的左侧和右侧,它们最终会缩小以适应.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.EqualWidthExample" >
<!-- An invisible view aligned to the center of the parent. Allows other
views to be arranged on either side -->
<View
android:id="@+id/centerShim"
android:layout_height="match_parent"
android:layout_width="0dp"
android:visibility="invisible"
android:layout_centerHorizontal="true"/>
<!--Set width to match_parent sets maximum width. alignParentLeft aligns
the left edge of this view with the left edge of its parent. toLeftOf
sets the right edge of this view to align with the left edge of the
given view. The result of all three settings is that this view will
always take up exactly half of the width of its parent, however wide
that may be. -->
<Button
android:id="@+id/btLeft"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/centerShim"
android:text="Left Button" />
<!--Same deal, but on the right -->
<Button
android:id="@+id/btRight"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/centerShim"
android:layout_below="@+id/tvLeft"
android:text="Right Button" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
2De*_*Dee 50
您可以将这两个RelativeLayouts放在一个水平方向的LinearLayout中,然后使用RelativeLayouts 的权重.这将将LinearLayout分成2个相等的部分.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:baselineAligned="false">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1">
</RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
现在您可以使用PercentRelativeLayout轻松完成此操作
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Button"
app:layout_widthPercent="50%"/>
<Button
android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@id/button"
android:text="Button 2"
app:layout_widthPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
Run Code Online (Sandbox Code Playgroud)
不要忘记添加gradle依赖项
dependencies {
compile 'com.android.support:percent:25.3.1'
}
Run Code Online (Sandbox Code Playgroud)
更新资料
自API级别26.0.0起不推荐使用PercentRelativeLayout
| 归档时间: |
|
| 查看次数: |
66615 次 |
| 最近记录: |