Listview中不能在imageview視圖下面對齊文本。以下是我使用的代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="@+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/icon"
android:layout_alignParentBottom="true"
android:gravity="center"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:textColor="@color/white"/>
</RelativeLayout>
ListView是這樣的:
<ListView android:id="@+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="280sp"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_toLeftOf="@+id/imgView"
android:scrollbars="none"
android:divider="@null"
android:gravity="center" >
</ListView>
從數組中用java代碼來動態地添加文本和圖像:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.gridview_layout, null);
holder = new ViewHolder();
holder.imageName = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageName.setText(name[position]);
holder.image.setImageResource(mThumbIds[position]);
return convertView;
}
Imageview下面的textview還是沒有居中對齊。請大家幫我解決這個問題。
在TextView中使用layout_centerHorizontal屬性:
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/icon"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:textColor="@color/white" />