我希望 ImageView 和 TextView 在我的 ViewHolder 中居中。
我点击它时的视觉表现:
它被推到左侧,所以当我点击它时,它也会“突出显示”图像视图的右侧。
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final ViewHolder holder;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
convertView = inflater.inflate(R.layout.item_level_single, null, true);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.myImageView);
holder.number = (TextView) convertView
.findViewById(R.id.myImageViewText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setImageResource(mThumbIds[position]);
if (position == 0){
holder.number.setText("1");
}if (position == 1){
holder.number.setText("2");
}if (position == 2){
holder.number.setText("3");
}if (position == 3){
holder.number.setText("4");
}if (position == 4){
holder.number.setText("5");
}if (position == 5){
holder.number.setText("6");
}if (position == 6){
holder.number.setText("7");
}if (position == 7){
holder.number.setText("8");
}if (position == 8){
holder.number.setText("9");
}
return convertView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round,
};
private static class ViewHolder{
public ImageView image;
public TextView number;
}
}
item_single.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:id="@+id/single"
>
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_oval_big_white"
/>
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myImageView"
android:layout_alignTop="@+id/myImageView"
android:layout_alignRight="@+id/myImageView"
android:layout_alignBottom="@+id/myImageView"
android:gravity="center"
android:text="1"
android:textColor="#000000" />
</RelativeLayout>
编辑 - 添加了item_single.xml
答案 0 :(得分:1)
RelativeLayout
的孩子必须拥有财产centerInParent
= true。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/single">
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_oval_big_white"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/myImageViewText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myImageView"
android:layout_alignTop="@+id/myImageView"
android:layout_alignRight="@+id/myImageView"
android:layout_alignBottom="@+id/myImageView"
android:gravity="center"
android:text="1"
android:textColor="#000000"
android:layout_centerInParent="true" />
</RelativeLayout>
答案 1 :(得分:1)
在 ImageView 部分中添加以下属性。
android:layout_centerHorizontal="true"
android:layout_gravity="center"
如果为true,则将此子项水平居中于其父级。
修改强>
您可以尝试使用
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="center"
android:src="@drawable/your_image"
/>
答案 2 :(得分:0)
尝试在ImageView中添加:
android:layout_gravity="center_vertical"
答案 3 :(得分:0)
在ImageView和TextView上添加此行
android:layout_centerInParent="true"
答案 4 :(得分:0)
使用以下代码更新您的<ImageView>
:
<ImageView
android:id="@+id/myImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_oval_big_white"
/>
答案 5 :(得分:0)
@ H.Brooks看看你是否从ListView使用过onItemClickListener并且没有使用只有Image的点击监听器,那么就会发生这种情况。我建议你改变你的适配器如下:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(mContext);
final ViewHolder holder;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
convertView = inflater.inflate(R.layout.item_level_single, null, true);
holder = new ViewHolder();
holder.image = (ImageView) convertView
.findViewById(R.id.myImageView);
holder.number = (TextView) convertView
.findViewById(R.id.myImageViewText);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (position == 0){
holder.number.setText("1");
}if (position == 1){
holder.number.setText("2");
}if (position == 2){
holder.number.setText("3");
}if (position == 3){
holder.number.setText("4");
}if (position == 4){
holder.number.setText("5");
}if (position == 5){
holder.number.setText("6");
}if (position == 6){
holder.number.setText("7");
}if (position == 7){
holder.number.setText("8");
}if (position == 8){
holder.number.setText("9");
}
}
});
holder.image.setImageResource(mThumbIds[position]);
return convertView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round, R.drawable.ic_round,
R.drawable.ic_round,
};
private static class ViewHolder{
public ImageView image;
public TextView number;
}
}