2015-11-06 82 views
1

我想在表行programmatically内添加LinearLayout。线性布局以XML中的重心为中心。但它示出了在表行的左侧如下所示TableRow中的LinearLayout不尊重分配的重力

TableLayout以下定义:

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="SELECT A VEHICLE" 
     android:textColor="@color/select_vehicle" 
     android:id="@+id/select_vehicle" 
     android:textSize="28sp" 
     android:textAppearance="?android:attr/textAppearanceSearchResultTitle" 
     android:layout_marginTop="@dimen/activity_horizontal_margin" 
     android:layout_centerHorizontal="true" /> 


<TableLayout 
    android:layout_width="fill_parent" 
    android:id="@+id/select_vehicle_table" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/select_vehicle" 
    android:layout_margin="@dimen/activity_horizontal_margin"> 


</TableLayout> 

</RelativeLayout> 

这里是线性布局是一个表行的子视图:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:gravity="center_horizontal" 
    > 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/vehicle_name" 
     android:paddingTop="10dp" 
     android:gravity="center" 
     android:text="Vehicle Name" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:paddingBottom="10dp" 
     android:id="@+id/vehicle_number" 
     android:gravity="center" 
     android:paddingTop="5dp" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="Vehicle Number" /> 

</LinearLayout> 

我这是在编程设定的方式,但中心的重力没有显示出来:

mTableLayout = (TableLayout) findViewById(R.id.select_vehicle_table); 
     LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     for(int i=0;i<2;i++) 
     { 

      TableRow tableRow = new TableRow(this); 
      tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT,Gravity.END)); 
      tableRow.setBackgroundResource(R.drawable.row_border); 


      View v = inflater.inflate(R.layout.table_row_linear_layout,null); 
      TextView vehicle_name = (TextView) v.findViewById(R.id.vehicle_name); 

      vehicle_name.setText(vehicle_name_array[i]); 
      TextView vehicle_number = (TextView) v.findViewById(R.id.vehicle_number); 
      vehicle_number.setText(vehicle_number_array[i]); 

      tableRow.addView(v); 
      mTableLayout.addView(tableRow); 
     } 
+0

要在哪里显示文字浏览?水平居中还是垂直居中? – Shvet

+0

水平居中 – AnupamChugh

+0

将Textview的宽度设置为'wrap_content'并检查 – Shvet

回答

1

我错过了这段代码,同时以编程方式设置LL

tableRow.setGravity(Gravity.CENTER_HORIZONTAL);