[英]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.