首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >android程序设计

android程序设计
EN

Stack Overflow用户
提问于 2014-01-17 18:00:20
回答 2查看 124关注 0票数 0

我正在android中构建一个简单的两列列表,并且需要动态填充行数。xml布局工作正常,但当我尝试以编程方式构建布局时,第二列将不会显示。有什么想法吗?

代码语言:javascript
复制
private void addRow(String text, String value) {
    RelativeLayout line = new RelativeLayout(this);

    line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

    RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    rlp.addRule(RelativeLayout.CENTER_VERTICAL);

    line.setPadding(0, 0, 0, 15);

    TextView caption = new TextView(this);
    caption.setLayoutParams(rlp);
    caption.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
    caption.setText(text);

    rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    rlp.addRule(RelativeLayout.CENTER_VERTICAL);

    TextView val = new TextView(this);
    val.setLayoutParams(rlp);
    val.setTextColor(0x05b4e4);
    val.setTextSize(TypedValue.COMPLEX_UNIT_PX, 30);
    val.setText(value);

    line.addView(caption);
    line.addView(val);

    mLayout.addView(line);
}

单行(工作)的可比xml如下所示

代码语言:javascript
复制
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingBottom="15px" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30px"
            android:text="What"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            />

        <TextView
            android:id="@+id/txtClubPath"
            android:textColor="#05b4e4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30px"
            android:text="stuff"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            />

    </RelativeLayout>

同样,行填充,但第二列不可见。下面是它的内容:

代码语言:javascript
复制
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/scrollViewRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:keepScreenOn="true">

<LinearLayout
    android:id="@+id/viewRoot"
    android:paddingTop="20px"
    android:paddingBottom="40px"
    android:paddingLeft="20px"
    android:paddingRight="20px"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

</LinearLayout>

</ScrollView>
EN

回答 2

Stack Overflow用户

发布于 2014-01-17 18:06:40

您要添加的视图的布局参数应该是LinearLayout.LayoutParams类型,因为父视图将是该类型的。

这条线

代码语言:javascript
复制
line.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));

应该变成

代码语言:javascript
复制
line.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,  LinearLayout.LayoutParams.WRAP_CONTENT));

不过,我认为这会导致例外情况。

票数 0
EN

Stack Overflow用户

发布于 2014-01-17 18:56:05

对于RelativeLayout孩子来说,有一个唯一的非零id是很重要的.

您可以在API 17+上使用

代码语言:javascript
复制
myView.setId(View.generateViewId());

或者,如果您必须支持较早的API级别,则可以使用(每次递增)一个简单的静态AtomicInteger值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21192732

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档