简体   繁体   中英

Android - LayoutInflater - Cant make textview stay below another textview

I am using a LayoutInflater to fill out a grid view using a GridAdapter, but when i try to place a textView below another textView within the layout i am inflating, it ends up being above my textView instead of below. When i change it to layout_above instead, it doesnt even appear on the screen. Layout that i inflate:

<?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>

My java code where i am inflating:

@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;
}

How it looks in the preview: Preview

But this is how it look in practice: In practice

You also need to handle personalTag visibility when the id is 0, because it may not be visible when the view is being recycled:

if(id == 0){
    bookName.setText(books[i]);
    personalTag.setVisibility(View.VISIBLE);
} else {
    bookName.setText(context.getString(id));
    personalTag.setVisibility(View.GONE);
}

Also avoid setting your root RelativeLayout with match_parent as height, they usually break in AdapterViews , try wrap_content or any hard values, or adjust your layout:

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM