简体   繁体   English

Android 根据适配器中的屏幕旋转/宽度改变高度

[英]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:每个 Pokemon Team 行的大小固定为 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:每行中的 Pokemon 是在此适配器中创建的图像视图:

  @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:我希望至少有 200dp 的高度以使每一行更大(否则我认为在平板电脑等设备中它看起来会非常小),如下所示:

在此处输入图像描述

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您还可以检查方向并相应地设置高度https://stackoverflow.com/a/2799001/12506486

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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