【问题标题】:Custom grid with strange proportions具有奇怪比例的自定义网格
【发布时间】:2018-01-28 13:19:10
【问题描述】:

我创建了一个自定义网格,在特定情况下,它必须具有 2x2 图像。 我的 XML 代码是这样的:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2">

<LinearLayout
    android:layout_weight="1"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <ImageView
        android:id="@+id/iv_immagine_sx1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@mipmap/cibo"
        android:scaleType="centerCrop"/>

    <ImageView
        android:id="@+id/iv_immagine_dx1"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@mipmap/cibo"
        android:scaleType="centerCrop" />

</LinearLayout>

<LinearLayout
    android:layout_weight="1"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="0dp">

    <ImageView
        android:id="@+id/iv_immagine_sx2"
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@mipmap/cibo"
        android:scaleType="centerCrop"/>

    <ImageView
        android:layout_weight="1"
        android:id="@+id/iv_immagine_dx2"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:src="@mipmap/cibo"
        android:scaleType="centerCrop" />

</LinearLayout></LinearLayout>

我会获得一个对称网格,其中每行具有相同的宽度/高度,每列具有相同的宽度/高度。

问题是在物理设备上运行代码我有这种情况如下图。

行的高度不同...为什么? 我该如何解决这个问题?

感谢您的帮助。


编辑:

根据@Rashiq 的建议,我将 weight_sum 添加到水平 LinearLayouts 但尚未修复。

正如@S Praveen Kumar 所建议的,我尝试将 TabLayout 与代​​码一起使用:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="160dp"
android:gravity="center"
android:orientation="vertical" >


<TableLayout
    android:id="@+id/tableLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher"/>

        <ImageView
            android:id="@+id/ImageView2"
            android:layout_width="0dp"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:src="@mipmap/ic_launcher"/>
    </TableRow>


    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1" >

        <ImageView
            android:id="@+id/ImageView3"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@mipmap/cibo" />

        <ImageView
            android:id="@+id/ImageView4"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:src="@mipmap/cibo" />
    </TableRow>

</TableLayout>

但是没有消息,问题依旧。

但请注意,我可以看到 当图像源具有不同大小时会出现问题... 我该如何解决?

【问题讨论】:

  • 只是一个建议,你为什么不使用 TableLayout 来代替?
  • 将 weight_sum 指定为水平线性布局并检查它是否解决了您的问题...否则尝试 tablelayout
  • @SPraveenKumar 感谢您的帮助,我已经编辑了我的帖子。问题依然存在。
  • @Rashiq,谢谢,我也编辑了我的帖子作为对您建议的回答。
  • @ClaudioP 我对此表示怀疑。您应该尝试对 imageView 使用 scaleType。试试 fitCenter 或 fitXY。

标签: android xml layout android-layout-weight


【解决方案1】:

希望这能解决您的问题:

首先找到父布局的宽度

ViewTreeObserver observer = parentLayout.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {

        @Override
        public void onGlobalLayout() {
            // TODO Auto-generated method stub
            int width = parentLayout.getWidth();
            parentLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);

            //initiate your grid here

        }
    });

将你的grid child layout的宽度和高度设置为如上得到的宽度,得到一个正方形的图像。

【讨论】:

    猜你喜欢
    • 2019-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-23
    • 2016-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多