Android - LayoutInflater - 无法使 textview 保持在另一个 textview 下方
Android - LayoutInflater - Cant make textview stay below another textview
我正在使用 LayoutInflater 使用 GridAdapter 填充网格视图,但是当我尝试将 textView 放置在我正在膨胀的布局中的另一个 textView 下方时,它最终位于我的 textView 上方而不是下方。当我改为 layout_above 时,它甚至没有出现在屏幕上。
我膨胀的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/menuBookModel">
<ImageView
android:id="@+id/bookImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/bookName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/personalTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bookName"
android:text="(Personal)"
android:layout_centerHorizontal="true"
android:textColor="@color/colorPrimaryDark"
android:textSize="15sp" />
</RelativeLayout>
我的 java 我充气的代码:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view == null){
view = inflater.inflate(R.layout.menu_book_model, null);
}
TextView bookName = view.findViewById(R.id.bookName);
TextView personalTag = view.findViewById(R.id.personalTag);
ImageView bookImage = view.findViewById(R.id.bookImage);
bookImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = String.valueOf(view.getTag());
aListener.chooseBook(s);
}
});
int id = context.getResources().getIdentifier(books[i], "string", context.getPackageName());
//if id == 0 the book is custom, and therefore it has the custom name
if(id == 0){
bookName.setText(books[i]);
} else {
bookName.setText(context.getString(id));
personalTag.setVisibility(View.GONE);
}
bookImage.setBackgroundColor(Color.rgb(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
bookImage.setTag(books[i]);
return view;
}
它在预览中的样子:
Preview
但实际情况是这样的:
In practice
当id
为0时,您还需要处理personalTag
可见性,因为在回收视图时它可能不可见:
if(id == 0){
bookName.setText(books[i]);
personalTag.setVisibility(View.VISIBLE);
} else {
bookName.setText(context.getString(id));
personalTag.setVisibility(View.GONE);
}
同时避免将根 RelativeLayout
设置为 match_parent
作为高度,它们通常 break in AdapterViews
,尝试 wrap_content
或任何硬值,或调整您的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/menuBookModel">
<ImageView
android:id="@+id/bookImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/bookName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:id="@+id/personalTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bookName"
android:layout_centerHorizontal="true"
android:text="(Personal)"
android:textColor="@color/colorPrimaryDark"
android:textSize="15sp" />
</RelativeLayout>
我正在使用 LayoutInflater 使用 GridAdapter 填充网格视图,但是当我尝试将 textView 放置在我正在膨胀的布局中的另一个 textView 下方时,它最终位于我的 textView 上方而不是下方。当我改为 layout_above 时,它甚至没有出现在屏幕上。 我膨胀的布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="@+id/menuBookModel">
<ImageView
android:id="@+id/bookImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/bookName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/personalTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bookName"
android:text="(Personal)"
android:layout_centerHorizontal="true"
android:textColor="@color/colorPrimaryDark"
android:textSize="15sp" />
</RelativeLayout>
我的 java 我充气的代码:
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(view == null){
view = inflater.inflate(R.layout.menu_book_model, null);
}
TextView bookName = view.findViewById(R.id.bookName);
TextView personalTag = view.findViewById(R.id.personalTag);
ImageView bookImage = view.findViewById(R.id.bookImage);
bookImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String s = String.valueOf(view.getTag());
aListener.chooseBook(s);
}
});
int id = context.getResources().getIdentifier(books[i], "string", context.getPackageName());
//if id == 0 the book is custom, and therefore it has the custom name
if(id == 0){
bookName.setText(books[i]);
} else {
bookName.setText(context.getString(id));
personalTag.setVisibility(View.GONE);
}
bookImage.setBackgroundColor(Color.rgb(random.nextInt(255),random.nextInt(255),random.nextInt(255)));
bookImage.setTag(books[i]);
return view;
}
它在预览中的样子: Preview
但实际情况是这样的: In practice
当id
为0时,您还需要处理personalTag
可见性,因为在回收视图时它可能不可见:
if(id == 0){
bookName.setText(books[i]);
personalTag.setVisibility(View.VISIBLE);
} else {
bookName.setText(context.getString(id));
personalTag.setVisibility(View.GONE);
}
同时避免将根 RelativeLayout
设置为 match_parent
作为高度,它们通常 break in AdapterViews
,尝试 wrap_content
或任何硬值,或调整您的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/menuBookModel">
<ImageView
android:id="@+id/bookImage"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_margin="5dp"/>
<TextView
android:id="@+id/bookName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test Text"
android:layout_centerInParent="true"
android:textSize="20sp"
android:textColor="@color/colorPrimaryDark"/>
<TextView
android:id="@+id/personalTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/bookName"
android:layout_centerHorizontal="true"
android:text="(Personal)"
android:textColor="@color/colorPrimaryDark"
android:textSize="15sp" />
</RelativeLayout>