[英]How to make TableLayout center in ScrollView for android
我正在尝试在ScrollView
中心显示表格,但是没有用。 但是,如果删除ScrollView
,该表将显示在中间。 我不知道那是我的错误。 谁能帮助您找到错误?
这是xml编码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_rekod"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.asiqin.attendance.Rekod"
android:background="#E9F1F2">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2">
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
</TableLayout>
</ScrollView>
</RelativeLayout>
这是java代码:
TableRow rowHeader = new TableRow(context);
rowHeader.setBackgroundColor(Color.parseColor("#c0c0c0"));
rowHeader.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));
String[] headertext = {"TARIKH","ALASAN","STATUS"};
for(String c:headertext){
TextView tv = new TextView(this);
tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
tv.setGravity(Gravity.CENTER);
tv.setTextSize(15);
tv.setPadding(5,5,5,5);
tv.setText(c);
// tv.setBackgroundDrawable(border1);
tv.setBackgroundResource(R.drawable.valuecellborder);
rowHeader.addView(tv);
}
您可以通过两种方式实现自己想要达到的目标
1)在scrollview内部使用相对布局
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:showDividers="middle">
</TableLayout>
</RelativeLayout>
</ScrollView>
2)在表格布局中使用android:layout_gravity =“ center”
<TableLayout
android:id="@+id/table"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:shrinkColumns="1"
android:divider="?android:attr/dividerHorizontal"
android:layout_gravity="center"
android:showDividers="middle">
您的代码存在问题,因为您使用的是android:layout_centerHorizontal =“ true”,当父版面是相对版面时,它可以工作,但如果父版面是ScrollView,则无法使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.