2012-11-12 82 views
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次)。

对于这篇长文章的道歉,我想提供足够的信息。非常感谢你提前。

更新:我试图张贴图片,但该网站说,我没有足够老到:)

克雷格

回答

1

在猜测,该RelativeLayout是无法处理WRAP_CONTENT高度设置你尝试给它,因为它具有与其底部对齐的子元素。正因为如此,它与父母的身高相符,因此它几乎占据了整个屏幕。尝试在rl布局参数中指定明确的高度,或者重新构造它的子项以避免任何ALIGN_PARENT_BOTTOM的使用 - 从我所能看到的情况来看,它应该足够简单,以便将imlay设置为刚好对齐(使用它自己的默认高度或显式指定你自己的),并且tv1lay使用ALIGN_BOTTOM规则,其ID为iv

+0

你钉了它!这就是它。删除WRAP_CONTENT并添加一个值来修复它。非常感谢你。现在要弄清楚为什么一些滚动功能还没有工作。再次感谢。 – mornindew

+0

很高兴能有所帮助 - RelativeLayouts有很多技巧和细微的要求,即使是非常有经验的人也可以绊倒。请记住标记答案,如果它解决了你的问题:) – Xono

相关问题