【问题标题】:Square layout not working in landscape方形布局不适用于横向
【发布时间】:2018-02-03 18:53:02
【问题描述】:

为什么我的方形视图不能在横向模式下工作?

它有以下onMeasure方法:

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int width = MeasureSpec.getSize(widthMeasureSpec);
    int height = MeasureSpec.getSize(heightMeasureSpec);
    size = Math.min(width, height);
    setMeasuredDimension(size, size);
}

在这里您可以看到我视图的背景(灰色),并且我的视图不是正方形的。为什么?

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="@dimen/md_dialog_frame_margin">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal">

        <com.my.package.GridPreviewView
            android:id="@+id/grid"
            android:background="#cccccc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/npCols"
            android:layout_alignParentTop="true"
            android:layout_marginBottom="8dp"
            android:layout_marginLeft="8dp"
            android:layout_toRightOf="@+id/npRows" />

        <com.shawnlin.numberpicker.NumberPicker
            android:id="@+id/npRows"
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/grid"
            android:layout_alignTop="@+id/grid"
            android:paddingRight="8dp"
            app:np_dividerColor="?attr/colorAccent"
            app:np_orientation="vertical"
            app:np_selectedTextColor="?attr/colorAccent"
            app:np_selectedTextSize="22sp"
            app:np_textSize="18sp"
            app:np_width="48dp" />

        <com.shawnlin.numberpicker.NumberPicker
            android:id="@+id/npCols"
            android:layout_width="wrap_content"
            android:layout_height="48dp"
            android:layout_alignLeft="@+id/grid"
            android:layout_alignParentBottom="true"
            android:layout_alignRight="@+id/grid"
            android:paddingTop="8dp"
            app:np_dividerColor="?attr/colorAccent"
            app:np_height="48dp"
            app:np_orientation="horizontal"
            app:np_selectedTextColor="?attr/colorAccent"
            app:np_selectedTextSize="22sp"
            app:np_textSize="18sp" />

    </RelativeLayout>
</LinearLayout>

【问题讨论】:

标签: android view android-custom-view custom-view


【解决方案1】:

我不确定您使用的 onMeasure() 技术可能存在什么问题,但请查看 SquareLayout,这是一个 Android 库,它为不同的布局提供了一个包装类,并呈现它们在不丢失任何核心功能的情况下进行平方尺寸。

尺寸是在布局渲染之前计算的,因此一旦获得视图,就没有重新渲染或任何类似的调整。这适用于所有场景(包括视图方向更改

要使用库,请将其添加到您的 build.gradle:

repositories {
    maven {
        url "https://maven.google.com"
    }
}

dependencies {
    compile 'com.github.kaushikthedeveloper:squarelayout:0.0.3'
}

注意:XML 预览暂时可能会损坏,但在运行应用程序时可以正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-10
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多