【发布时间】:2014-02-10 15:49:26
【问题描述】:
我为 TableLayout 使用了动态宽度和高度,并且我使用了 android:layout_gravity="center" 但我的 tablelayout 仍然保留在左侧。我找不到任何理由。请通过我下面的代码。
JAVA代码-:
Display display = getWindowManager().getDefaultDisplay();
int width=0;
int height=0;
if(display.getWidth()<=240 && display.getHeight()<=400)
{
width = 200;
height = 500;
}
else
{
width=250;
height=400;
}
TableLayout ab = (TableLayout) findViewById(R.id.tbl);
ab.setLayoutParams(new FrameLayout.LayoutParams(width,height));
XML 代码-:
<TableLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
android:id="@+id/tbl"
android:layout_gravity="center"
android:background="#ffffcc">
<TableRow>
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="2">
</TextView>
</TableRow>
<TableRow>
<TextView
android:id="@+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_span="2">
</TextView>
</TableRow>
两个 textview 元素在左边距对齐。但我希望它在中心。
请给我一些好的解决方案。!!!
谢谢。
【问题讨论】:
标签: android xml tablelayout