繁体   English   中英

如何调整图像大小以适合按钮?

[英]How to make image resize to fit buttons?

我现在的应用

如您所见,图片使第一个按钮不合比例地膨胀。 如何调整图片的大小以使其适合按钮

我希望它看起来像这样

圆圈将缩小并适合按钮的位置

这是我的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.anthonypan.myapplication.MainActivity">


    <TableLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">

        <TableRow>

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:textColor="@color/red" />

            <Button
                android:id="@+id/button2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:textColor="@color/red" />

            <Button
                android:id="@+id/button3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:background="@drawable/selector"
                android:textColor="@color/red" />
        </TableRow>
    </TableLayout>


</RelativeLayout>

添加它而不是表。

            <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>

您设置按钮大小包装内容,这意味着按钮大小将取决于图像大小。 更好的是,您可以固定按钮的大小。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.anthonypan.myapplication.MainActivity">
<TableLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true">

    <TableRow>

        <Button
            android:id="@+id/button"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/selector"
            android:textColor="@color/red" />

        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/selector"
            android:textColor="@color/red" />

        <Button
            android:id="@+id/button3"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:background="@drawable/selector"
            android:textColor="@color/red" />
    </TableRow>
</TableLayout>

在所需的屏幕截图中,您似乎希望所有按钮都占据屏幕的1/3 您可以通过首先获取屏幕尺寸来轻松实现此目的,如下所示:

Display display = getWindowManager().getDefaultDisplay(); // Your screen
Point size = new Point();
display.getSize(size);
int screenWidth = size.x; // The width of your screen
int screenHeight = size.y; // The height of your screen

接下来,获取图像的目标尺寸和宽度:

int imageWidth = imageView.getWidth();
float width = screenWidth / 3;
float scaleFactor = width / imageWidth; // This gets the scale factor to get the height
float height = imageView.getHeight() * scaleFactor;

您现在可以像这样设置ImageButton的尺寸:

imageView.requestLayout();
imageView.getLayoutParams().height = height;
imageView.getLayoutParams().width = width;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM