【问题标题】:How to perfect grid view on all types of device?如何在所有类型的设备上完善网格视图?
【发布时间】:2017-03-24 16:23:01
【问题描述】:

我正在尝试构建一个应用程序,其中从顶部开始 40% 的屏幕是 viewflipper,60% 的屏幕是大小为 9(9 个卡片视图)的卡片视图网格。 每个卡片视图都有一个图像视图和一个文本视图。 我正在使用 recyclerview 和 gridlayoutmanager 来填充网格。

我面临的问题是,当我在不同设备上运行应用程序时,网格并不一致,在一个设备上它完全适合屏幕,但在另一台设备上剩余空间,而在另一台设备上它就用完了屏幕,所以我必须上下滚动。我希望 9 个卡片视图在所有设备上都显示相同。

在另一台设备上,最后一行会离开屏幕,并且边距也会出错。 Java文件

public class HomeScreen extends AppCompatActivity {
    private CardView cardView;
    private RecyclerView recyclerView;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager LayoutManager;
    private ArrayList<Grid> list = new ArrayList<Grid>();
    private int[] image_id = {R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher,
            R.mipmap.ic_launcher, R.mipmap.ic_launcher, R.mipmap.ic_launcher};
    private String[] name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_screen);


        percentRelativeLayout = findViewById(R.id.activity_home_screen);

        name = getResources().getStringArray(R.array.persons_name);
        int count = 0;
        for (String Name : name) {
            Grid contact = new Grid(image_id[count], Name);
            count++;
            list.add(contact);
        }


        recyclerView = (RecyclerView)
findViewById(R.id.recyclerView_home_screen);
        LayoutManager = new GridLayoutManager(this, 3);
        recyclerView.setLayoutManager(LayoutManager);
        recyclerView.setHasFixedSize(true);
        adapter = new GridAdaptar(list);
        recyclerView.setAdapter(adapter);
    }
}

卡片 xml

<?xml version="1.0" encoding="utf-8"?>


<android.support.v7.widget.CardView android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/homescreen_card_view"
    android:clickable="true"
    android:focusable="true"
    android:foreground="?android:attr/selectableItemBackground"
    android:elevation="100dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="10dp"
    xmlns:android="http://schemas.android.com/apk/res/android">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="10dp"
            android:background="#eaece5"
            android:gravity="center"
            >
            <ImageView
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:src="@mipmap/ic_launcher"
                android:id="@+id/person_image"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true" />

            <TextView
                android:text=""
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/person_name"
                android:gravity="center"

                android:textSize="7dp"
                android:layout_below="@+id/person_image" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>

主屏幕xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_home_screen">

    <ViewFlipper
        android:id="@+id/viewFlipper_main"
        android:layout_width="match_parent"
        app:layout_heightPercent="40%"
        android:autoStart="true"
        android:flipInterval="2000"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        >

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/img1"
            android:adjustViewBounds="false"
            android:scaleType="centerCrop" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/img2"
            android:scaleType="centerCrop" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/img3"
            android:scaleType="centerCrop" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/img4"
            android:scaleType="centerCrop" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@drawable/img5"
            android:scaleType="centerCrop" />
    </ViewFlipper>
    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        app:layout_heightPercent="60%"
        android:layout_below="@+id/viewFlipper_main"
        android:id="@+id/recyclerView_home_screen"> </android.support.v7.widget.RecyclerView>

</android.support.percent.PercentRelativeLayout>

【问题讨论】:

  • 所以你总是想要一个 3x3 的网格来完全填满 60% 的屏幕?这意味着项目的宽度/高度适应屏幕尺寸
  • 是的,总是 3x3,60% 的屏幕。卡片应调整为填充屏幕的 60%。 @MehmetKologlu

标签: java android android-layout android-recyclerview


【解决方案1】:

出现问题是因为您在 cardItem 中提供了特定大小的 imageView。所以 cardItem 的总大小可能会超过设备大小的 60%。 当您对 cardItem 使用 RelativeLayout 时,请尝试使用权重而不是特定大小 (60dp)。

【讨论】:

  • 你能解释一下吗?使用 LinearLayout 的权重似乎不起作用。保留 wrap_content 时的图像高度/宽度不起作用,因为它最终取决于图像大小。
  • 如果权重不起作用,那么您可以按照以下步骤操作: 1. 将 wrap_content 设置为高度和宽度。 2.不要在xml中使用android:src。 3. 在developer.android.com/topic/performance/graphics/… 链接的帮助下,根据设备的大小调整java文件中的图像大小。 4. 将调整后的图片设置为imageview。
  • 感谢您的所有帮助。但它最终不会起作用,因为它最终取决于我的图像大小,即它在不同屏幕分辨率上显示的大小,即使我使用图像资产为不同的图像提供相同的图像分辨率它仍然不完美,总是有一些空间。您的建议是两张卡之间的间距不能解决问题。
猜你喜欢
  • 2020-02-06
  • 2021-02-22
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 2012-12-24
相关资源
最近更新 更多