android - TableLayout(TableRow?)在动态添加时未按预期调整 subview 的大小

标签 android android-layout

上下文: 我有一个 TableLayout(使用 XML 创建),它有一个 TableRow,它有一个 TextView。代码:

 <ScrollView 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:fillViewport="true"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
  <TableLayout
     android:id="@+id/mytable"
     android:orientation="vertical"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:stretchColumns="1"
     >
    <TableRow>
      <TextView
     android:id="@+id/add_alarm"
     android:layout_weight="1"
     android:layout_width="0dp"
     android:layout_height="fill_parent"
     android:gravity="center"
     android:text="New\nItem"
     android:textSize="30sp"
     />
    </TableRow>
  </TableLayout>
</ScrollView>

在我的 Activity 的 onCreate() 方法中,我试图将另一个 View 动态添加到 TableRow。这是代码:

    public void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
        LayoutInflater inflater = LayoutInflater.from(this);
        View mainLayout = inflater.inflate(R.layout.main, null);
        TableLayout tl = (TableLayout) mainLayout.findViewById(R.id.mytable);

        TableRow tr = (TableRow) tl.getChildAt(0);
        Log.d(TAG, "tr class = " + tr.getClass().getName() + " | width = " + tr.getWidth() + "\n");
        final RelativeLayout rl = (RelativeLayout) inflater.inflate(R.layout.alarm_widget, null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
        tr.addView(rl, lp); 
        tl.invalidate();
        setContentView(mainLayout);
}

问题:此代码没有在等宽的列中显示两个 View (一个已经在 XML 布局中,另一个是动态添加的)的预期效果。

  1. 通过上面给出的代码,动态添加的 View 的宽度为“0”,因此是不可见的。
  2. 如果我将代码更改为 tr.addView(rl)(即不引用 LayoutParams),动态添加的 View 是可见的,但列的宽度不相等。

我怎样才能做到这一点?

编辑:我根据评论将代码更改为以下内容。它仍然没有按预期工作:

TableLayout.LayoutParams lp = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
                               TableLayout.LayoutParams.WRAP_CONTENT, 1f);
tr.addView(rl, lp); 

最佳答案

问题是为 TableRow 定义的这种行为:

The children of a TableRow do not need to specify the layout_width and layout_height attributes in the XML file. TableRow always enforces those values to be respectively MATCH_PARENT and WRAP_CONTENT.

与其将 TextView 直接添加到 TableRow,不如让 TableRow 持有一个水平的 LinearLayout 并将第二个 View 添加到该持有者。

(此外,将 LinearLayout.LayoutParams 用于进入 TableRow 的内容是错误的。您应该一直使用 TableRow.LayoutParams 。但这不是获得等宽 TextView 的方法。使用 LinearLayout 支架。)

关于android - TableLayout(TableRow?)在动态添加时未按预期调整 subview 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8538113/

相关文章:

android - android 中的警报对话框

android - 添加项目以在 Android 中以编程方式查看

Android 布局问题 - 在不同设备上显示不同。

android - listView 的自定义 View - 字体

android - 如何沿 Viewpager 显示圆形标签指示器?

android - 在首次启动 Android 应用程序时从不同的 Activity 开始

android - Logcat 在收听桌面时钟警报 Intent 时无法在广播接收器的 onReceive 内部工作

android - 难以理解 layout_alignWithParentIfMissing

php - 从 php 数据库中获取一组 JSON 数据

android singleLine vs maxLines