我有一个包含一对并排按钮的RelativeLayout,我想在布局中居中.我可以将按钮放在LinearLayout中并将其放在RelativeLayout中,但我希望尽可能保持我的xml干净.
这是我尝试过的,这只是将"apply"按钮放在中间,将"undo"按钮放在它的左边:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15sp">
<TextView
android:id="@+id/instructions"
android:text="@string/instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15sp"
android:layout_centerHorizontal="true"
/>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/apply"
android:textSize="20sp"
android:layout_below="@id/instructions"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="@string/undo"
android:textSize="20sp"
android:layout_toLeftOf="@id/apply"
android:layout_below="@id/instructions"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
Bra*_*ash 62
android:gravity
将对齐内部view
或内容layout
使用的内容.
android:layout_gravity
将调整view
或layout
他的父母的内部.
所以加入
android:gravity="center"
Run Code Online (Sandbox Code Playgroud)
你的RelativeLayout应该做的伎俩......
像这样:
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="15sp">
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
min*_*iot 10
这是BrainCrash答案的扩展.它是一个非嵌套选项,可以水平和垂直地对所有三个进行分组和居中.此外,它采用顶部TextView并在两个按钮上水平居中.如果需要,您可以使用android:gravity ="center"将文本置于TextView中心.我还删除了边距,添加了颜色,并将RelativeLayout高度设置为fill_parent以突出显示布局.在API 11上测试过.
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@android:color/black"
>
<TextView
android:id="@+id/instructions"
android:text="TEST"
android:textSize="20sp"
android:background="@android:color/darker_gray"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignLeft="@+id/undo"
android:layout_alignRight="@+id/apply"
android:gravity="center"
/>
<Button
android:id="@+id/apply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="APPLY"
android:textSize="20sp"
android:layout_below="@id/instructions"
/>
<Button
android:id="@+id/undo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="UNDO"
android:textSize="20sp"
android:layout_toLeftOf="@id/apply"
android:layout_below="@id/instructions"
/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
40469 次 |
最近记录: |