2013-04-16 157 views
0

其實我試圖在滾動視圖中動態添加線性佈局。我想對待像行一樣的線性佈局,並且一行包含imageView和TexView,所以我想在每次調用該函數時動態添加它們。所以它可能看起來像一個listview ..我很累,因爲搜索每一個地方。請幫我在滾動視圖中動態添加線性佈局

enter image description here

<ScrollView 
    android:id="@+id/scroll" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/relativeLayout1" 
    android:layout_above="@+id/footer" 
    > 

<LinearLayout 
    android:id="@+id/innerpart" 
     android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="horizontal" > 

     </LinearLayout> 
</ScrollView> 



    try{ 
     //get bitmap image 
     imageview img = new imageview(getAppcontext); 
     linear.addview(img); 

     // but i want to add image and row in the same row 
     } 
+0

你試過了什麼? – Sebastian

+0

其實我試圖創建一個具有固定位置的imageview和textview的佈局,以便它能夠看起來像行。然後我想添加布局,我需要放一排 –

+0

我的意思是,在Stackoverflow它總是更好地發佈一些代碼或標記,你已經寫了 – Sebastian

回答

1

你在這裏,所以你可以動態地添加LinerLayout以滾動型:

for loop { 
    LinearLayout newLL = new LinearLayout(context, attrs //layout position params); 
    ImageView newIV = new ImageView(context, attrs); 
    TextView newTV = new TextView(context, attrs); 
    newLL.addView(newIV); 
    newLL.addView(newTV); 

    yourScrollView.add(newLL); 
} 

這裏您問的風格,看起來像:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/list_selector" 
android:orientation="horizontal" 
android:padding="5dip" > 

<!-- ListRow Left sied Thumbnail image --> 
<LinearLayout android:id="@+id/thumbnail" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:padding="3dip"  
    android:layout_alignParentLeft="true" 
    android:background="@drawable/image_bg" 
    android:layout_marginRight="5dip"> 

    <ImageView  
     android:id="@+id/list_image" 
     android:layout_width="50dip" 
     android:layout_height="50dip" 
     android:src="@drawable/rihanna"/> 

</LinearLayout> 

<!-- Title Of Song--> 
<TextView 
    android:id="@+id/title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/thumbnail" 
    android:layout_toRightOf="@+id/thumbnail" 
    android:text="Rihanna Love the way lie" 
    android:textColor="#040404" 
    android:typeface="sans" 
    android:textSize="15dip" 
    android:textStyle="bold"/> 

<!-- Artist Name --> 
<TextView 
    android:id="@+id/artist" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@id/title" 
    android:textColor="#343434" 
    android:textSize="10dip" 
    android:layout_marginTop="1dip" 
    android:layout_toRightOf="@+id/thumbnail" 
    android:text="Just gona stand there and ..." /> 

<!-- Rightend Duration --> 
<TextView 
    android:id="@+id/duration" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentRight="true" 
    android:layout_alignTop="@id/title" 
    android:gravity="right" 
    android:text="5:45" 
    android:layout_marginRight="5dip" 
    android:textSize="10dip" 
    android:textColor="#10bcc9" 
    android:textStyle="bold"/> 

<!-- Rightend Arrow -->  
<ImageView android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/arrow" 
    android:layout_alignParentRight="true" 
    android:layout_centerVertical="true"/> 

</RelativeLayout> 

佈局屏幕截圖:

enter image description here

+0

我們可以在固定位置添加imageview和textView嗎? –

+0

示例:您可以爲每個佈局參數創建新的LayoutParams(WRAP_CONTENT,FILL_PARENT,Gravity.CENTER),並將其設置爲imageView和textView –

+0

可以建議我如何創建此佈局? http://tinypic.com/view.php?pic=2ii8zus&s=6 –

相關問題