[英]View won't show when layout is inflated
這是我的list_section.xml代碼:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:background="@color/colorBackgroundDark"
android:paddingBottom="10dp"
android:paddingTop="10dp">
<TextView
android:id="@+id/textViewAssessmentTypeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="16dp"
android:textColor="@color/colorPrimaryText" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8000FF00" />
</RelativeLayout>
我通過以下方式在RecyclerViewAdapter中對其進行充氣:
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
...
else if (viewType == TYPE_SECTION) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_section, parent, false);
return new ViewHolderSection(view, callback);
}
...
}
private class ViewHolderSection extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
ItemClickListener callback;
TextView assessmentTypeName;
ViewHolderSection(View view, ItemClickListener callback) {
super(view);
this.callback = callback;
assessmentTypeName = (TextView) view.findViewById(R.id.textViewAssessmentTypeName);
view.setOnClickListener(this);
view.setOnLongClickListener(this);
}
@Override
public void onClick(View v) {
if (callback != null) {
callback.onItemClick(getAdapterPosition());
}
}
@Override
public boolean onLongClick(View v) {
if (callback != null) {
callback.onItemLongClick(getAdapterPosition());
}
return true;
}
}
但是由於某些原因,我不會顯示layout.xml中的View(我將其用作RelativeLayout的彩色濾鏡/疊加層)。 可能是什么問題呢?
我通過將list_section.xml更改為:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="@color/colorBackgroundDark">
<TextView
android:id="@+id/textViewAssessmentTypeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="left"
android:paddingLeft="16dp"
android:textColor="@color/colorPrimaryText" />
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#8000FF00" />
</RelativeLayout>
前一個無效,因為RelativeLayout的layout_height最終為0。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.