代码之家  ›  专栏  ›  技术社区  ›  Robby Smet

需要以线性布局为中心的帮助

  •  0
  • Robby Smet  · 技术社区  · 14 年前

    我有以下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_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>
    

    这将产生以下输出:

    enter image description here

    现在我想把日期选择器放在橙色容器的中心。我将属性设置为居中,但这只是将其水平居中。

    我只能使用linearlayout,所以请不要建议使用相对布局:)

    我已经为此奋斗了一段时间,所以任何帮助都将不胜感激!

    Thx公司

    5 回复  |  直到 14 年前
        1
  •  2
  •   Aprian    14 年前

    去除 android:layout_gravity 在你的 timepicker orange container 并设置 android:gravity="center" 到您的 橙色集装箱 使其居中。

    android:gravity 定义 childs 虽然 layout_gravity 定义 layout/view 它本身

        2
  •  0
  •   Sahil Mahajan Mj ila123    14 年前

    尝试

    android:layout_gravity="center_vertical"
    
        3
  •  0
  •   rajeshwaran    14 年前

    使用这个,

    <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" />
    
        4
  •  0
  •   Benito Bertoli    14 年前

    安卓:布局_重力 孩子可以提供给父母的标准重力常数。

    安卓:重力 指定如何将对象的内容放置在x上- 以及y轴。

    所以在你的橙色 LinearLayout 使用 android:gravity 而不是 android:layout_gravity

        5
  •  0
  •   Sanath    14 年前

    将时间选择器的父linearlayout的重力设置为中心,如下所示

        <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" >