如何在水平滚动视图下显示图像

时间:2017-01-13 10:06:11

标签: android horizontalscrollview

我在android中创建了一个水平滚动视图,滚动浏览图像。我想在每个图像下方显示文本,但无论我做什么,我只能在图像上或右侧显示文本。例如,每当我使用android:layout_below命令时,文本就会完全消失。有没有人知道为什么会发生这种情况以及如何让它在图像下正确显示?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.selwyn.ciaran.cookbook.IngredientsActivity">


    <include android:id="@+id/app_bar"
        layout="@layout/app_bar" />


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Ingredients"
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_below="@+id/app_bar"
        android:layout_centerHorizontal="true" />

    <HorizontalScrollView
        android:layout_below ="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="100dp"
        android:id="@+id/horizontalScrollView"
        android:fillViewport="true"
        android:measureAllChildren="false"
        android:scrollbars="none">

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

        </LinearLayout>

    </HorizontalScrollView>
</RelativeLayout>

我的cell.xml文件

<RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

            <ImageView android:id="@+id/_image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"/>

            <TextView
                android:id="@+id/_imageName"
                android:layout_height="wrap_content"
                android:layout_below="@id/_image"
                android:layout_width="wrap_content"
                android:layout_centerHorizontal="true"
                android:textSize="16sp"
                android:gravity="center"
                android:textColor="#FFFFFF"/>
</RelativeLayout>

我的成分活动

public class IngredientsActivity extends AppCompatActivity {

    private LinearLayout mainLayout;

    private int[] ingredients;
    private int[] images = {R.drawable.oj, R.drawable.oj, R.drawable.oj,
            R.drawable.oj, R.drawable.oj, R.drawable.oj, R.drawable.oj};

    private View cell;
    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ingredients);
        toolbar = (Toolbar) findViewById(R.id.app_bar);
        setSupportActionBar(toolbar);
        mainLayout = (LinearLayout) findViewById(R.id._linearLayout);

        for (int i = 0; i < images.length; i++) {

            cell = getLayoutInflater().inflate(R.layout.cell, null);
            text = (TextView) cell.findViewById(R.id._imageName);
            final ImageView imageView = (ImageView) cell.findViewById(R.id._image);
            imageView.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // do whatever you want ...
                    Toast.makeText(IngredientsActivity.this,
                            (CharSequence) imageView.getTag(), Toast.LENGTH_SHORT).show();
                }
            });
            imageView.setTag("Image#" + (i + 1));



            imageView.setImageResource(images[i]);
            text.setText("Image#" + (i + 1));

            mainLayout.addView(cell);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

加入cell.xml

<LinearLayout
    android:id="@+id/_linearLayout"
    android:layout_width="wrap_content"  //<----here
    android:layout_height="match_parent"  //<---here
    android:orientation="horizontal">

  <include layout="@layout/cell"/>

</LinearLayout>

您的cell.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<ImageView android:id="@+id/_image"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@mipmap/ic_launcher"
    android:layout_marginBottom="20dp"
    android:layout_weight="0.5"/>

<TextView
    android:id="@+id/_imageName"
    android:layout_height="25dp"
    android:layout_width="match_parent"
    android:text="dummy"
    android:gravity="center"
    android:textSize="16sp"
    android:textColor="#000"/>
</LinearLayout>

如果在HorizontalLayout中加入,您的视图会自动匹配:enter image description here

答案 1 :(得分:0)

您可以使用修改后的xml文件

main_activity.xml

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


  <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textAppearance="?android:attr/textAppearanceMedium"
      android:text="Ingredients"
      android:id="@+id/textView"
      android:gravity="center"
      android:layout_centerHorizontal="true" />

  <HorizontalScrollView
      android:layout_below ="@+id/textView"
      android:layout_width="fill_parent"
      android:layout_height="100dp"
      android:id="@+id/horizontalScrollView"
      android:fillViewport="true"
      android:measureAllChildren="false"
      android:scrollbars="none">

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

      <include layout="@layout/cell"/>

    </LinearLayout>

  </HorizontalScrollView>
</RelativeLayout>

cell.xml

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

    <ImageView android:id="@+id/_image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:id="@+id/_imageName"
        android:layout_height="match_parent"
        android:layout_width="wrap_content"
        android:text="dummy"
        android:layout_gravity="center"
        android:textSize="16sp"
        android:textColor="#000"/>
</LinearLayout>