我知道我们可以为android:gravity和 android:layout_gravity属性设置以下值:
centercenter_verticalcenter_horizontal等但我对这两个问题感到困惑.
使用android:gravity和有android:layout_gravity什么区别?
我试图在RelativeLayout一个基础上水平居中几个视图.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal"
android:background="@android:color/transparent" >
Run Code Online (Sandbox Code Playgroud)
这不起作用.我已centerInParent到true了一个视图,并且做了工作.但是,我无法使用此解决方案,因为我有两个并排的视图需要集中在一起.试图优化这个,所以我想避免嵌套布局,特别是线性,彼此内部.
有什么明显的东西让我失踪吗?我认为这个属性是针对这种情况做出的.
我有一个具有以下布局的活动:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutOverlays"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/testlayout_bottom"
android:layout_width="fill_parent"
android:layout_height="62dp"
android:background="#122334" >
<ImageView
android:id="@+id/testbtnBlock"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_gravity="left|center_vertical"
android:contentDescription="Test1"
android:padding="@dimen/padding_medium"
android:src="@drawable/btnblock" />
<TextView
android:id="@+id/testtxtZoomPan"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/testbtnX"
android:layout_toRightOf="@+id/testbtnBlock"
android:gravity="center_horizontal"
android:text="@string/txtZoomPan"
android:textColor="#FFFFFF" />
<ImageView
android:id="@+id/testbtnX"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_gravity="right|center_vertical"
android:contentDescription="Test2"
android:padding="@dimen/padding_medium"
android:src="@drawable/btnx" />
</RelativeLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutPuzzleInfo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/testlayoutChronoErrors"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Chronometer
android:id="@+id/testchronometer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:format="@string/chronometer_initial_format"
android:gravity="center" />
<LinearLayout …Run Code Online (Sandbox Code Playgroud)