简体   繁体   中英

Android change height depending on screen rotation /width in Adapter

I have the following layout:

Each of the Pokemon Team rows has a fixed size of 100dp:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pokemonTeambuilderContainer"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical">

    <TextView
        android:id="@+id/pokemonTeambuilderTitleTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="@string/untitled"
        android:textSize="16sp"
        android:textStyle="bold" />

    <LinearLayout
        android:id="@+id/pokemonTeambuilderSpritesLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="horizontal" />

</LinearLayout>
</androidx.cardview.widget.CardView>

在此处输入图像描述

The Pokemon inside each row are image view created in this adapter:

  @Override
public void onBindViewHolder(ViewHolder holder, int position) {
    PokemonTeam pokemonTeam = mData.get(position);

    for (Pokemon pokemon : pokemonTeam.getPokemonList()) {
        CircularImageView ivPokemonSprite = new CircularImageView(mContext);
        int color = PokemonUtils.getDominantColorFromPokemon(pokemon.get_id(), mContext);
        ivPokemonSprite.setBorderColor(color);
        ivPokemonSprite.setBorderWidth(4);

        ivPokemonSprite.setCircleColor(PokemonUtils.lighterColor(color, DARK_FACTOR));
        Picasso.get().load(PokemonUtils.getPokemonSugimoriImageById(pokemon.get_id(), mContext)).fit().centerCrop().into(ivPokemonSprite);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
        layoutParams.setMargins(3, 10, 3, 10);
        ivPokemonSprite.setLayoutParams(layoutParams);
        holder.teamSpritesLinearLayout.addView(ivPokemonSprite);
    }

    String pokemonTeamName = pokemonTeam.getName();
    if (pokemonTeamName != null && !pokemonTeamName.isEmpty()) {
        holder.teamTitleTextView.setText(pokemonTeam.getName());
    }

}

My problem is when I rotate the device:

在此处输入图像描述

I would like to have at least 200dp of height in order to make each row bigger (otherwise I think that in devices like Tablet it will look very small), like this:

在此处输入图像描述

you can have alternative layouts for different devices, I think that's the most reliable way https://developer.android.com/guide/practices/screens_support

You can also check the orientation and set up the height accordingly https://stackoverflow.com/a/2799001/12506486

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