【问题标题】:How do you add dynamically a view from xml file, I want to add TableRow in RelativeLayout如何从xml文件动态添加视图,我想在RelativeLayout中添加TableRow
【发布时间】:2011-03-12 20:26:41
【问题描述】:

我正在尝试在我的活动中动态添加表格行。表格行处于相对布局中。它看起来不错,但不知道我哪里出错了。下面是我的代码

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
    TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
    for(int i = 1; i <3; i++)
        RLayout.addView(tableRow); //My code is crashing here
}

而main.xml如下

  <?xml version="1.0" encoding="utf-8"?>
  <RelativeLayout
  android:id="@+id/RelativeLayout"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  xmlns:android="http://schemas.android.com/apk/res/android"
  >
  <TableRow
  android:id="@+id/TableRow"
  android:layout_width="fill_parent"
  android:layout_height="wrap_content"
  android:orientation="horizontal"
  android:layout_alignParentTop="true"
  android:layout_alignParentLeft="true"
  >
  <TextView
  android:id="@+id/Text"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="Text"
  >
  </TextView>
  </TableRow>
  </RelativeLayout>

请帮忙。

【问题讨论】:

    标签: android tablerow


    【解决方案1】:

    它崩溃了,因为 TableRow 已经在布局中。如果你想动态添加一些,你必须以编程方式创建它,即:

    // PSEUDOCODE
    TableRow newRow = new TableRow(this);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(/*....*/);
    newRow.setLayoutParams(lp);
    
    relLayout.add(newRow);
    

    其实TableRow应该用在TableLayout里面。

    如果您想多次使用某些东西,可以使用膨胀技术。您需要创建一个 xml 布局,其中包含您要重复的唯一部分(因此您的 TableRow 及其子项),然后:

    LayoutInflater inflater = 
                  (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    
    View inflated = inflater.inflate(R.layout.your_layout, null);
    

    现在inflated 包含您指定的布局。而不是null,您可能希望将布局放置在其中附加膨胀的布局。每次你需要一个这样的新元素时,你都必须以同样的方式给它充气。

    (您应该始终报告崩溃时遇到的错误)

    ----- 编辑 -----

    好的,我明白了,这是你的代码:

    RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
    TableRow tableRow = (TableRow)findViewById(R.id.TableRow);
    
    for(int i = 1; i <4; i++) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View inflated = inflater.inflate(R.layout.main, tableRow);
    }
    

    这样您就可以在原始 TableRow 中扩展整个布局。

    你应该有一个像这样的row.xml 布局,以及main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <TableRow xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/TableRow"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      >
      <TextView
      android:id="@+id/Text"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Text"
      />
      </TableRow>
    

    然后像这样充气:

    RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout);
    
    for(int i = 1; i <4; i++) {
        LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.row, RLayout);
    }
    

    看看它是否有效。

    【讨论】:

    • 感谢您提供此信息。试用后我会回来的。
    • 哇它有效,现在我也了解布局充气器了。谢谢。
    • 发生了一个问题。添加的行水平附加。我希望新行低于前一行。我以为我可能只需要改变方向,但事实并非如此......
    • @Shaista 您是否将它们添加到 TableLayout 而不是 RelativeLayout?如果由于某些(奇怪)原因您仍在使用 RelativeLayout,请记住您必须附加到您添加 LayoutParams 的每个孩子,并设置它们以解释您希望它们如何处理。
    • 这里是代码RelativeLayout RLayout = (RelativeLayout)findViewById(R.id.RelativeLayout); TableRow tableRow = (TableRow)findViewById(R.id.TableRow); for(int i = 1; i
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多