代码之家  ›  专栏  ›  技术社区  ›  Jean Valjean

android:表行与父宽度不匹配

  •  0
  • Jean Valjean  · 技术社区  · 6 年前

    我有一个滚动的tablelayout,它应该在游戏的各个级别上显示用户的进度。除了将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;
        }
    

    这些方法在充当表容器的片段类中找到。我知道桌子本身的宽度是对的,但行不是。

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

    1 回复  |  直到 6 年前
        1
  •  0
  •   Psypher    6 年前

    您需要在TableLayout容器中添加TableRow,而不是没有它。如下所示更改布局文件,添加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">
           ............