2016-04-30 72 views
0

我想添加兩個子視圖到水平滾動視圖。這兩個視圖是ImageViewTextViewTextView應該低於`ImageView,這兩個視圖都應該水平滾動是否可以實現此目的?如何添加?我是android新手。horizo​​ntalscrollview與圖像和文字可以添加兩個視圖

在此先感謝。

在我的片段:

LinearLayout lv = (LinearLayout) v.findViewById(R.id.textl); 
     for (int i=0;i<5;i++){ 
      ImageView iv = new ImageView(getContext()); 
      TextView tv = new TextView(getContext()); 
      tv.setText(text[i]); //defined text and images 
      iv.setImageResource(images[i]); 
      lv.addView(iv); 
      lv.addView(tv); 

XML:

<HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_marginTop="10dp" 
     android:id="@+id/hs"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/bottle" 
      android:orientation="horizontal"> 

      <LinearLayout 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:orientation="vertical" 
       android:id="@+id/textl"/> 

     </LinearLayout> 

    </HorizontalScrollView> 

回答

1

你應該使用的LinearLayout與垂直方向水平滾動視圖中,然後使用圖片和文字視圖中的LinearLayout。

<HorizontalScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/hsv" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    android:orientation="vertical" > 
    <ImageView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/to" /> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="@string/subject" /> 


</LinearLayout> 
</HorizontalScrollView> 
+0

好,我會盡力感謝您迴應 – sun

+0

當我添加圖片和文字我有錯誤有指定的孩子已經有一位家長。您必須先調用子對象的父對象的removeView()。 – sun

+0

請你分享一下你的java文件。 –

0

所有這三個問題的答案:

的TextView下面的ImageView,如下圖所示,

是的,這是可能的

喲可以檢查以下XML代碼

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:layout_width="match_parent" 
       android:layout_height="100dip"> 

    <HorizontalScrollView 
     android:id="@+id/hsv" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:fillViewport="true" 
     android:measureAllChildren="false" 
     android:scrollbars="none"> 

     <LinearLayout 
      android:id="@+id/innerLay" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:gravity="center_vertical" 
      android:orientation="horizontal"> 

      <ImageView 
       android:id="@+id/iV1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:src="@drawable/next_"/> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="match_parent" 
       android:layout_below="@+id/iV1" 
       android:gravity="center" 
       android:hint="hello"/> 
     </LinearLayout> 
    </HorizontalScrollView> 
</RelativeLayout> 
+0

我想如果我們像這樣添加,我認爲它會出錯 - 指定的孩子已經有一個父母。您必須在向其中添加文本和圖像時首先調用孩子父母的removeView() – sun

+0

不,它不會拋出錯誤,請檢查http://stackoverflow.com/questions/18656949/how-to-implement-horizo​​ntalscrollview- like-gallery –

+0

是的我動態地將圖像和文本添加到imageview和textview中,但它出現問題指定的子項已經有父項。您必須先調用子對象的父對象的removeView()。 – sun

0

試試這個:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <HorizontalScrollView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:orientation="vertical"> 

      <ImageView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:padding="2dp" 
       android:text="Hello" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_marginTop="3dp" 
       android:padding="2dp" 
       android:text="Hello" 
       android:textSize="20sp" /> 

     </LinearLayout> 
    </HorizontalScrollView> 
</LinearLayout> 
1

儘可能最好的方式是使用RecyclerView。這是使用它的最好方式,因爲它可以自行擴展,並且元素也可以隨時加載。 如果你在活動然後在的onCreate方法

// recyclerView is id mentioned in xml file 
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView); 

LinearLayoutManager mLayoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false); 

mRecyclerView.setLayoutManager(mLayoutManager); 

** your adapter init goes here ** 
mRecyclerView.setAdapter(**adapter object**); 

有關詳細信息,發佈您的代碼 - 將幫助你。對於片段,你需要做同樣略有改動

注意:你也應該在XML文件中定義recyclerview

相關問題