2012-11-12 162 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

相關問題