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);
}
要在哪裏顯示文字瀏覽?水平居中還是垂直居中? – Shvet
水平居中 – AnupamChugh
將Textview的寬度設置爲'wrap_content'並檢查 – Shvet