我正在尝试在表格行LinearLayout
中添加programmatically
。线性布局在XML中以重力为中心。但它显示在表格行的左侧,如下所示
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 :(得分:1)
我在以编程方式设置LL时错过了这行代码
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);