【问题标题】:Android: Tablerows not matching parent widthAndroid:表格行与父宽度不匹配
【发布时间】:2018-12-18 23:03:36
【问题描述】:

我有一个滚动表格布局,它应该显示用户在游戏中各个级别的进度。一切正常,除了行宽似乎是包装内容,尽管 xml 宽度设置为 match_parent。我需要动态添加表数据。这是我将行添加到 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;
    }

这些方法可以在充当表格容器的片段类中找到。我知道表格本身的宽度是正确的,但行不是。

从 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 的宽度与父级的宽度相匹配。

【问题讨论】:

    标签: android xml view


    【解决方案1】:

    您需要在 TableLayout 容器中添加 TableRow,而不是没有。如下更改您的布局文件,将 TableLayout 添加为父布局,并且在 TableLayout 中的每一行都应对应 对于RelativeLayout,您不需要android:orientation="horizontal",这适用于LinearLayout 之类的布局

    <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">
           ............
    

    【讨论】:

    • 但是如果我像这样添加表格行,我无法以编程方式控制行数,对吧?
    • 在您的情况下添加回收视图或列表视图不是更好的方法吗?