1

我遇到了LinearLayout的问题。我在LinearLayout中有两个项目..一个是包裹宽度的Linearlayout,另一个是简单的textview ...我的包装宽度LinearLayout从Activity获取宽度..并且它工作良好..但是我有一个textview问题,它是第二个项目..它从我的包裹的LinearLayout,无论宽度我裹布局得到,从TextView的启动它。我要做出的TextView在中心对齐相对于家长开始Linearlayout.Here是我的XML ..`如何在Android中使用LinerLayout中的父项对齐第二项?

     <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="0dp" 
          android:layout_weight="1" 
          android:background="@drawable/graph_panel_bg_one" 
          android:orientation="horizontal" > 

          <LinearLayout 
           android:id="@+id/looser_left_jab_force_panel" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:background="@drawable/force_panel_gradient" > 
          </LinearLayout> 

          <TextView 
           android:id="@+id/looser_left_jab_force" 
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content" 
           android:gravity="center" 
           android:text="" 
           android:textColor="#F18B86" 
           android:textSize="10sp" /> 
         </LinearLayout> 

         <LinearLayout 
          android:layout_width="match_parent" 
          android:layout_height="0dp" 
          android:layout_weight="1" 
          android:background="@drawable/graph_panel_bg_one" > 

          <LinearLayout 
           android:id="@+id/looser_left_jab_speed_panel" 
           android:layout_width="wrap_content" 
           android:layout_height="match_parent" 
           android:background="@drawable/speed_panel_gradient" > 
          </LinearLayout> 

          <TextView 
           android:id="@+id/looser_left_jab_speed" 
           android:layout_width="match_parent" 
           android:layout_height="match_parent" 
           android:gravity="center" 
           android:text="12222" 
           android:textColor="#CEE2E9" 
           android:textSize="10sp" /> 
         </LinearLayout> 
        </LinearLayout>` 
+2

更好地利用相对布局如果要对齐东西 – Giant

+0

变化'安卓重力=“中心”'到'机器人:layout_gravity =“中心”' – R9J

+0

是的,它是更好地使用相对布局,但现在我在条件,不能够将该线性转换为相对..任何其他帮助.. –

回答

1

的LinearLayout是对齐所有儿童在一个单一的方向,垂直地或水平的图组LY。您可以使用android:orientation属性指定布局方向。

LinearLayout的所有子项都依次堆叠在一起,所以垂直列表每行只有一个子项,不管它们有多宽,水平列表只会是一行高(高度最高的孩子,加上填充)。 LinearLayout尊重儿童之间的边距以及每个孩子的重力(右侧,中央或左侧对齐)。

它从我的包裹的LinearLayout,无论宽我的包裹布局得到

这是因为你的父母线性布局 “android:orientation="horizontal”,如果设置了“机器人启动:orientation =“vertical”它可以从你的包装的LinearLayout开始,无论你的包装布局获得什么样的高度。

可能很难通过LinearLayout实现您想要的方式。

我也建议去相对布局。按照你想要的方式对齐孩子是有帮助的。

注意:采取适当的行动你的textview可能与LinearLayout重叠。

+0

谢谢@vinaykumar –

1

您的TextView宽设置为"wrap_content"

相关问题