Android:表格行与父级宽度不匹配

Android:表格行与父级宽度不匹配,android,xml,view,Android,Xml,View,我有一个滚动的桌面布局,它应该显示游戏中不同级别的用户进度。一切正常,除了行宽度看起来是包装内容,尽管xml宽度被设置为与父项匹配。我需要动态添加表数据。以下是我将行添加到tableView的代码: public View getView(LayoutInflater inflater, Context context, int position) { View view = inflater.inflate(R.layout.prog_cell_layout, null

我有一个滚动的桌面布局,它应该显示游戏中不同级别的用户进度。一切正常,除了行宽度看起来是包装内容,尽管xml宽度被设置为与父项匹配。我需要动态添加表数据。以下是我将行添加到tableView的代码:

    public View getView(LayoutInflater inflater, Context context, int position) {
        View view = inflater.inflate(R.layout.prog_cell_layout, null);

        String letter = " " + (char)('a'  + position) + " ";
        TextView tv = (TextView)view.findViewById(R.id.progLetterName);
        tv.setText(letter);

        return view;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_progress, container, false);

        //It starts here. There are 26 rows
        TableLayout tab = (TableLayout)rootView.findViewById(R.id.tableView);

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

            View conView = getView(inflater, getContext(), i);


            TableRow.LayoutParams lp = new TableRow.LayoutParams(
                    TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT
            );
            conView.setLayoutParams(lp);

            tab.addView(conView);

        }

        return rootView;
    }
public View getView(布局、充气器、上下文、int位置){
视图=充气机。充气(R.layout.prog\u cell\u layout,空);
字符串字母=”+(字符)('a'+位置)+”;
TextView tv=(TextView)view.findViewById(R.id.progLetterName);
tv.setText(字母);
返回视图;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u进度,容器,false);
//从这里开始,一共有26排
TableLayout选项卡=(TableLayout)rootView.findviewbyd(R.id.tableView);
对于(int i=0;i<26;i++){
View conView=getView(充气机,getContext(),i);
TableRow.LayoutParams lp=新建TableRow.LayoutParams(
TableRow.LayoutParams.MATCH_父级,TableRow.LayoutParams.MATCH_父级
);
conView.setLayoutParams(lp);
tab.addView(conView);
}
返回rootView;
}
这些方法位于充当表容器的片段类中。我知道桌子本身的宽度是正确的,但行不是

这些行是从xml布局中读取的:

<?xml version="1.0" encoding="utf-8"?>
<TableRow android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_dark"
        >

        <TextView
            android:id="@+id/progLetterName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"

            android:layout_marginLeft="10dp"
            android:text="a"
            android:textSize="20dp"
            android:fontFamily="sans-serif-light"
            android:textAlignment="center"
            />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/black"
            android:text="TextView"
            android:textSize="20sp"
            android:fontFamily="sans-serif-light"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>
</TableRow>


我尝试了很多方法……但显然没有一种方法能够使TableRow的宽度与父级的宽度相匹配。

您需要将TableRow添加到TableLayout容器中,而不是没有。按如下所示更改布局文件,将TableLayout添加为父布局,并在TableLayout中,每一行都应与之对应 对于相对布局,您不需要使用android:orientation=“horizontal”,这适用于线性布局之类的布局

<TableLayout android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <TableRow android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/holo_green_dark">
       ............

............

但是如果我像这样添加表行,我无法通过编程控制行数,对吗?在您的情况下,添加recyclerview或listview不是更好的方法吗?