以编程方式调整图像按钮的大小以使其保持正方形

时间:2017-03-01 00:31:50

标签: java android height android-linearlayout programmatically

我正在开展一个项目,我的挑战是让ImageButtons成为方形。我使用LinearLayouts,因此我使用layout_weight作为宽度,以便屏幕的宽度可以在ImageButtons之间平均分配,并预设layout_height "100dp" 1}}。我想现在以编程方式将layout_height更改为已分配的layout_width。我已经检查了其他问题但没有找到我要找的东西。请帮忙。 这是activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.abdul.sdgschampoins.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <LinearLayout
            android:id="@+id/layout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/goal1"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal1"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal2"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal2"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal3"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal3"
                android:layout_margin="4dp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/goal4"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal4"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal5"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal5"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal6"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal6"
                android:layout_margin="4dp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/goal7"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal7"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal8"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal8"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal9"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal9"
                android:layout_margin="4dp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/goal10"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal10"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal11"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal11"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal12"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal12"
                android:layout_margin="4dp"/>
        </LinearLayout>
        <LinearLayout
            android:id="@+id/layout5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/goal13"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal13"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal14"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal14"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal15"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal15"
                android:layout_margin="4dp"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/layout6"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageButton
                android:id="@+id/goal16"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal16"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/goal17"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/goal17"
                android:layout_margin="4dp"/>
            <ImageButton
                android:id="@+id/dummyIcon"
                android:layout_weight="1"
                android:layout_width="0dp"
                android:layout_height="105dp"
                android:background="@drawable/sdglogo"
                android:layout_margin="4dp"/>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

1 个答案:

答案 0 :(得分:0)

我认为你应该尝试这样的事情:

button.setLayoutParams(new LayoutParams(button.getWidth(), button.getWidth());
相关问题