Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android TableRow内的LinearLayout不考虑指定的重力_Android_Android Layout_Android Tablelayout - Fatal编程技术网

Android TableRow内的LinearLayout不考虑指定的重力

Android TableRow内的LinearLayout不考虑指定的重力,android,android-layout,android-tablelayout,Android,Android Layout,Android Tablelayout,我试图以编程方式在表行中添加线性布局。在XML中,线性布局以重力为中心。但它显示在表格行的左侧,如下所示 表格布局的定义如下: <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> <TextView

我试图以编程方式在表行
中添加
线性布局
。在XML中,线性布局以重力为中心。但它显示在表格行的左侧,如下所示

表格布局的定义如下:

    <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);
        }
mTableLayout=(TableLayout)findViewById(R.id.select\u vehicle\u table);
LayoutInflater充气器=(LayoutInflater)this.getSystemService(Context.LAYOUT\u充气器\u服务);

对于(inti=0;i我在以编程方式设置LL时错过了这一行代码

tableRow.setGravity(Gravity.CENTER_HORIZONTAL);

要在何处显示文本视图?水平居中还是垂直居中?水平居中将文本视图的宽度设置为
环绕内容
并选中谢谢,但这没有帮助。总之,我得到了它。只需在setLayoutParams之外设置tableRow的重力,如下所示:tableRow.setGravity(Gravity.center\u HORIZONTAL);