在scrollview中动态添加线性布局

时间:2013-04-16 07:23:49

标签: scrollview android-linearlayout

实际上我试图在滚动视图中动态添加线性布局。我想处理像行一样的线性布局,一行包含imageView和TexView,所以我想在每次调用该函数时动态添加它们。所以它可能看起来像一个列表视图..我很累,因为搜索每个地方。请帮帮我

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
       }

1 个答案:

答案 0 :(得分:1)

您在这里,因此您可以动态地将LinearLayout添加到ScrollView:

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