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

时间:2016-05-14 21:13:37

标签: android xml

My app right now

正如你所看到的那样,图片按比例增加了第一个按钮。如何调整图片大小以使其适合按钮

我希望它看起来更像this

圆圈缩小并适合按钮

这是我的代码:

<?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>

3 个答案:

答案 0 :(得分:0)

添加它而不是表格。

            <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>

答案 1 :(得分:0)

您可以设置按钮大小换行内容,这意味着按钮大小取决于图像大小。您可以更好地修复按钮大小。

<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>

答案 2 :(得分:0)

在您想要的屏幕截图中,您似乎希望所有按钮占据屏幕的 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;
相关问题