2017-01-13 92 views
-2

我在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); 
     } 
    } 
} 
+0

您將固定高度設置爲Horizo​​ntalScrollView,而不是將其賦予圖像視圖(在我的cell.xml文件中),然後嘗試使Horizo​​ntalScrollView高度變爲wrap_content。 –

+0

你可以發佈你想要的圖像嗎? –

+0

考慮接受答案,如果幫助..;) – W4R10CK

回答

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