java - 将 ImageButton 对齐以填充宽度并保持纵横比

标签 java android xml

我有一个像这样的 ImageButton:

<ImageButton
   android:background="@drawable/ctg_a"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:adjustViewBounds="true"
   android:scaleType="centerCrop"
   android:id="@+id/cmdCategoryA" />

我的布局在线性布局内有多个这样的 ImageButtons(嵌套在 ScrollView 内)。按钮应占用其宽度的 100% 可用空间,并使用匹配的高度,以便图像的比例 (5:3) 不会更改。

通过我的代码,我得到的图像在宽度上填充整个页面,但在高度上图像使用其原始尺寸,该尺寸太大并且与比例不匹配。

因此,目前图像的比例约为 5:8,而不是 5:3。如何降低高度?

编辑:这可能与上下文有关吗?这是我的 Activity 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.mjz.test.MainActivity">


<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/scrollView" >

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:background="@drawable/ctg_a"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:adjustViewBounds="true"
            android:id="@+id/cmdCategoryA"
            android:scaleType="centerCrop" />         
    </LinearLayout>
</ScrollView>
</LinearLayout>

我采用了另一种方法,即清除 xml,并动态添加按钮。我的代码:

ImageButton cmdCategoryA;
LinearLayout mainLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mainLayout = (LinearLayout)findViewById(R.id.categories_layout);
    setTitle("Kategorien");

    Configuration configuration = this.getResources().getConfiguration();
    int screenWidthDp = configuration.screenWidthDp;

    cmdCategoryA = new ImageButton(getApplicationContext());
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    layoutParams.gravity= Gravity.CENTER;
    layoutParams.width=screenWidthDp;
    layoutParams.height=(screenWidthDp/5)*3;
    cmdCategoryA.setLayoutParams(layoutParams);
    cmdCategoryA.setBackground(getDrawable(R.drawable.ctg_a));
    mainLayout.addView(cmdCategoryA);
}

这种方法可行,只是图像不是全宽,而是屏幕的 1/3。 screenWidthDp 返回 360,这对于我的 Nexus 5 来说是正确的。

我必须在某处添加类型(dp/px/...)吗?

最佳答案

我相信你不能仅用 xml 方式做到这一点......

首先将drawable转换为Bitmap

Bitmap image = BitmapFactory.decodeResource(context.getResources(),R.drawable.icon_resource);

然后尝试使用:

Bitmap resized = Bitmap.createScaledBitmap(yourBitmap,(int)(yourBitmap.getWidth()*0.8), (int)(yourBitmap.getHeight()*0.8), true);

我希望这有帮助..

已编辑:

也试试这个:

Bitmap resized = Bitmap.createScaledBitmap(image, newWidth, newHeight, true);

在尝试此操作时,不要将 imageview 的高度设置为 match_parent,而是将其设置为 warpcontent

关于java - 将 ImageButton 对齐以填充宽度并保持纵横比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36246284/

相关文章:

java - 如何在 Amazon Alexa SDK 中返回 Dialog.Delegate?

java - 即使在同步后仍存在竞争条件

java - 如何移动微调下拉菜单以便可以看到所选的选项?

xml - 将变量从 bash 传递到 XQuery

xml - s4s-att-不允许 : Attribute 'name' cannot appear in element 'simpleType'

java - 循环访问特定文件?

java - 用 Java 编写台球游戏 - 如何定义球并移动它们(通过 Graphics g)?

java - 将处理程序或线程与 while true 一起使用?

android - Robotium:请安装兼容的 Android API 级别(15 或更高)

java - 如何正确设置GridView?