Android 需要帮助在线性布局中居中吗
我有以下xml布局:Android 需要帮助在线性布局中居中吗,android,android-layout,Android,Android Layout,我有以下xml布局: <ScrollView android:layout_width="match_parent" android:layout_height="match_parent" android:fillViewport="true" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_par
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF99E6"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_marginTop="5dip"
android:text="Was patient"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="2dip"
android:background="#298EB5"
android:orientation="horizontal" >
</LinearLayout>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="7dip"
android:layout_marginTop="10dip"
android:text="Hier wat extra info"
android:textAppearance="?android:attr/textAppearanceMedium" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#FF6633"
android:orientation="vertical" >
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#298EE5"
android:orientation="horizontal" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</ScrollView>
这将产生以下输出:
现在我想把日期选择器放在橙色容器的中心。我将属性设置为居中,但这只会使其水平居中
我只能使用linearlayout,因此请不要建议使用相对布局:)
我已经为此绞尽脑汁一段时间了,所以任何帮助都将不胜感激
Thx试试看
android:layout_gravity="center_vertical"
移除
android:layout\u gravity
上的timepicker
和orange容器上的android:gravity=“center”
并将其设置为橙色容器的中心
android:gravity
定义childs
的重力,而layout\u gravity
定义布局/视图本身的重力。使用此选项
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#FF6633"
android:gravity="center"
android:orientation="vertical" >
<TimePicker
android:id="@+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
android:layout_gravity孩子可以提供给父母的标准重力常数
android:gravity指定如何将对象的内容放置在x轴上
和y轴,在对象本身内
因此,在橙色LinearLayout
中,使用android:gravity
而不是android:layou gravity
将父LinearLayout of time picker的重力设置为中心,如下所示
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#FF6633"
android:gravity="center"
android:orientation="vertical" >
使用RelativeLayout
解决这个问题,快速提问,你能解释为什么将布局重心设置为中心会显示与将子布局重心设置为中心不同的结果吗?我认为布局重心:居中会使子对象在其父对象内居中。这很有效,快速提问,你能解释为什么将布局重心设置为居中会显示与将子对象布局重心设置为居中不同的结果吗?布局重力:中心将使子对象在其父对象内居中。好的,如果线性布局
是水平方向
,则只能通过布局重力
定义子对象的垂直
对齐,反之亦然。这就是为什么将layout\u gravity
设置为center
在垂直方向的线性布局中无法按预期工作的原因(您无法定义儿童的垂直对齐)。