以编程方式创建相对布局

问题描述 投票:1回答:1

我是android的新手,我正在尝试以编程方式创建RelativeLayouts(至少我认为如果我的值是动态的,它将是唯一的工作方式)。基本上我构建了一个Web服务来返回值列表,我想显示这些值。它可以是一个值,也可以是500。

在我的layout.xml中,我能够构建一个相对接近我想要的RelativeLayout但我只能填充一次。下面是我的XML:

注意:这是整个XML,但他所讨论的节点是RelativeLayout。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    android:orientation="vertical" 
    android:id="@+id/linear">

    <Spinner
        android:id="@+id/band"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#000000"
        android:dropDownSelector="#000000"
        android:textColor="#F0F0F0" />

    <Spinner
        android:id="@+id/yearSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textColor="#F0F0F0" />

      <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight"
        android:padding="6dip"
        android:id="@+id/showRelativeLayout">

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="6dip"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/secondLine"
            android:layout_width="fill_parent"
            android:layout_height="26dip"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"
            android:layout_toRightOf="@id/icon"
            android:ellipsize="marquee"
            android:singleLine="true"
            android:text="Simple application that shows how to use RelativeLayout" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/secondLine"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_alignWithParentIfMissing="true"
            android:layout_toRightOf="@id/icon"
            android:gravity="center_vertical"
            android:text="My Application"
            android:id="@+id/thirdLine" />
    </RelativeLayout>


</LinearLayout>

所以我接着尝试在我的Activity中构建相同的内容,以便我可以遍历我的webservice结果。下面是多次调用的方法(对于每个返回的json数组)。不幸的是,行为是只有一个relativeLayout并且它试图占用屏幕的其余部分(一行约为90%)。我绝对不理解一些基本的东西。

private void processRelativeLayout(int count) {
    RelativeLayout rl = new RelativeLayout(this);
    RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    rl.setLayoutParams(lay);

    //Set the Image
    //TODO:  pull from webservice to get the artist image
    RelativeLayout.LayoutParams imlay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
    imlay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    imlay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    imlay.setMargins(0, 0, 6, 0);

    ImageView iv = new ImageView(this);
    iv.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
    iv.setId(1+count);
    iv.setLayoutParams(imlay);
    rl.addView(iv);

    RelativeLayout.LayoutParams tv1lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, 26);
    tv1lay.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    tv1lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv1lay.addRule(RelativeLayout.RIGHT_OF, 1+count);
    TextView tv = new TextView(this);
    tv.setSingleLine();
    tv.setEllipsize(TruncateAt.MARQUEE);
    tv.setTextColor(Color.YELLOW);
    tv.setText("Simple application that shows how to use RelativeLayout");
    tv.setId(2+count);
    tv.setLayoutParams(tv1lay);
    rl.addView(tv);

    RelativeLayout.LayoutParams tv2lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    tv2lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
    tv2lay.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
    tv2lay.addRule(RelativeLayout.ABOVE, 2+count);
    tv2lay.addRule(RelativeLayout.RIGHT_OF,1+count);
    TextView tv2 = new TextView(this);
    tv2.setTextColor(Color.YELLOW);
    tv2.setGravity(Gravity.CENTER_VERTICAL);
    tv2.setText("My Application");
    tv2.setId(count+3);
    tv2.setLayoutParams(tv2lay);
    rl.addView(tv2);

    rootLinear.addView(rl);


    //add the relative layoutll.addView(tv, lay);
}

我还附上了我的模拟器的屏幕截图,以显示输出。我本来希望看到结果5次(因为我调用了processRelativeLayout 5次)。

为长篇文章道歉,但我想提供足够的信息。

更新:我试图发布图片,但该网站说我没有足够的经验:)

android android-layout relativelayout dynamic-data
1个回答
1
投票

猜测,RelativeLayout无法处理你试图给它的WRAP_CONTENT高度设置,因为它有子元素与它的底部对齐。正因为如此,它反过来又回到匹配它的父母身高,因此它几乎占据了整个屏幕。尝试在rl布局参数中指定显式高度,或者重构它的子节点以避免使用任何ALIGN_PARENT_BOTTOM - 从我所看到的,它应该足够简单,将imlay设置为仅对齐(使用它自己的默认高度或显式指定你自己的一个),并让tv1lay使用ALIGN_BOTTOM规则与iv的id。

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.