【问题标题】:How to keep TableLayout in Center using Android?如何使用 Android 将 TableLayout 保持在中心?
【发布时间】: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


    【解决方案1】:

    尝试给予

    android:gravity="center" 
    

    在你的文本视图中

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 2012-01-25
      • 2019-08-31
      • 1970-01-01
      • 2012-03-01
      • 2023-03-19
      • 2015-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多