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

时间:2016-03-27 10:24:06

标签: 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" />

我的布局在linearlayout中嵌入了多个这些ImageButtons(嵌套在scrollView中)。按钮应占据其宽度的100%可用空间,并使用匹配的高度,因此图像的比例(5:3)不会改变。

使用我的代码,我得到的图像以宽度填充整个页面,但在高度上,图像使用其原始大小,该大小太大而且与比率不匹配。

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

编辑:它可能与上下文有关吗?以下是我的活动代码:

<?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 / ...)?

2 个答案:

答案 0 :(得分:1)

我相信你不能只用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

答案 1 :(得分:0)

我自己找到了一个合适的方法:

我决定以编程方式添加控件,并使用以下内容调整图像大小。

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

如果您不希望图像覆盖整个宽度,请将宽度和高度减小到例如90%。 同时使图像居中以获得良好的外观。

相关问题