horizo​​ntalscrollview与图像和文字可以添加两个视图

问题描述:

我想添加两个子视图到水平滚动视图。这两个视图是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> 

你应该使用的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文件。 –

所有这三个问题的答案:

的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

试试这个:

<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> 

尽可能最好的方式是使用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