我有一个滚动的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的宽度与家长的宽度相匹配。