我正在尝试在表行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);
}
最佳答案
我只是以编程方式设置LL而错过了这一行代码
tableRow.setGravity(Gravity.CENTER_HORIZONTAL);
关于android - TableRow中的LinearLayout不考虑分配的重力,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33561728/