【问题标题】:resizing an imagebutton programmatically to keep it square以编程方式调整图像按钮的大小以使其保持方形
【发布时间】:2017-07-20 03:48:43
【问题描述】:

我正在做一个项目,我的挑战是让ImageButtons 变成方形。我使用的是LinearLayouts,所以我使用layout_weight作为宽度,这样屏幕的宽度可以在ImageButtons"100dp"的预设layout_height之间平均分配。我现在想以编程方式将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>

【问题讨论】:

    标签: java android height android-linearlayout programmatically


    【解决方案1】:

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

    button.setLayoutParams(new LayoutParams(button.getWidth(), button.getWidth());
    

    【讨论】:

    • 我试过了,我试过的ImageButton从屏幕上消失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-11
    相关资源
    最近更新 更多