我在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);
}
}
}
您將固定高度設置爲HorizontalScrollView,而不是將其賦予圖像視圖(在我的cell.xml文件中),然後嘗試使HorizontalScrollView高度變爲wrap_content。 –
你可以發佈你想要的圖像嗎? –
考慮接受答案,如果幫助..;) – W4R10CK