如何根据屏幕尺寸制作方形按钮

时间:2018-01-21 18:57:39

标签: java android xml android-layout

我需要可滚动的布局,单行上有2个方形按钮。 (2 x N表) 宽度应取决于屏幕尺寸。 (屏幕宽度/ 2) 高度应该等于宽度。 (因为它是方形按钮)

2 个答案:

答案 0 :(得分:0)

我想,你想要这样的东西:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dll"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<GridLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_columnWeight="1"
        ></Button>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:layout_columnWeight="1"></Button>
</GridLayout>
</ScrollView>

答案 1 :(得分:0)

我找到了解决方案!它适用于高分辨率方形图像。也可滚动。

<?xml version="1.0" encoding="utf-8"?>
        <ScrollView android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:scrollbars="none"
            android:layout_weight="1"
            xmlns:android="http://schemas.android.com/apk/res/android">
            <TableLayout
                android:id="@+id/tableLayout1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:padding="0dp"
                android:isScrollContainer="true">

            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:weightSum="2">

                <ImageButton
                    android:background="@null"
                    android:id="@+id/imageButcton2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:cropToPadding="false"
                    android:scaleType="centerInside"
                    app:srcCompat="@drawable/coffee0" />

                <ImageButton
                    android:background="@null"
                    android:id="@+id/imageButzxton"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    app:srcCompat="@drawable/coffee0" />

            </TableRow>


            <TableRow
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:weightSum="2">

                <ImageButton
                    android:background="@null"
                    android:id="@+id/sdas"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    app:srcCompat="@drawable/coffee0" />

                <ImageButton
                    android:background="@null"
                    android:id="@+id/imageButsadasdastdon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    app:srcCompat="@drawable/coffee0" />

            </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:weightSum="2">

                    <ImageButton
                        android:background="@null"
                        android:id="@+id/imageBzxzzczxzutton2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"
                        app:srcCompat="@drawable/coffee0" />

                    <ImageButton
                        android:background="@null"
                        android:id="@+id/imageButtxzczxdon"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"
                        app:srcCompat="@drawable/coffee0" />

                </TableRow>    <TableRow
                android:layout_width="fill_parent"
                android:layout_height="match_parent"
                android:weightSum="2">

                <ImageButton
                    android:background="@null"
                    android:id="@+id/imagexzzczButton2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    app:srcCompat="@drawable/coffee0" />

                <ImageButton
                    android:background="@null"
                    android:id="@+id/imageButtdon"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:adjustViewBounds="true"
                    android:scaleType="centerInside"
                    app:srcCompat="@drawable/coffee0" />

            </TableRow>
                <TableRow
                    android:layout_width="fill_parent"
                    android:layout_height="match_parent"
                    android:weightSum="2">

                    <ImageButton
                        android:background="@null"
                        android:id="@+id/imaxzzczgeButton2"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"
                        app:srcCompat="@drawable/coffee0" />

                    <ImageButton
                        android:background="@null"
                        android:id="@+id/imagezxzcButtdon"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_weight="1"
                        android:adjustViewBounds="true"
                        android:scaleType="centerInside"
                        app:srcCompat="@drawable/coffee0" />

                </TableRow>


        </TableLayout>
        </ScrollView>