0

这是我的Button

convertView = mInflater.inflate(R.layout.guide_view_image_item, null);
viewHolder = new ViewHolder();
viewHolder.myButton = (Button) convertView.findViewById(R.id.btn_goto_loginview);

这是guide_view_image_item.xml里面btn_goto_loginview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:gravity="center"
              android:orientation="vertical" >

    <ImageView
            android:id="@+id/imgView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_delete" >

    </ImageView>

    <Button
            android:id="@+id/btn_goto_loginview"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/skep_button_in_guide_page"
            android:layout_alignParentBottom="true"
            android:layout_alignParentRight="true"/>

</RelativeLayout>

skep_button_in_guide_page 是 182*182 PNG(32 位颜色)6.10Kb 图像

我的程序运行良好,但是 myButton 显示得太大了!(我的 ScreenWidth 是 480,这个按钮的宽度覆盖了我的 ScreenWidth 的一半)

然后我用

RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams();

我发现myButtonLayout.width&都myButtonLayout.height等于 -2,我想这可能意味着 WRAP_CONTENT。然后我用

BitmapFactory.Options buttonOptions = new BitmapFactory.Options();
imageOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(EngageApp.getIns().getResources(), R.drawable.skep_button_normal, buttonOptions);

然后我发现buttonOptions.outWidth&buttonOptions.outHeight都等于 273 所以我认为这是屏幕上显示的实际(真实)宽度。奇怪了,273是从哪里来的?应该是182..

最后,我必须使用这个:

RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams();
myButtonLayout.width = 182;
myButtonLayout.height = 182;
viewHolder.myButton.setLayoutParams(myButtonLayout);

强制将宽度和高度设置为 182

然后,一切正常,myButton 变小了,这正是我的目的。但我不认为这是解决这个问题的正确方法。

任何人都可以帮助我吗??提前致谢。


也许我的描述令人困惑,让我说得更清楚。

首先,我的图像是 182 宽度并且我使用 WRAP_CONTENT,为什么我的按钮没有直接在 480 宽度的屏幕上显示为 182 宽度。

其次,为什么我在使用“decodeResource”时得到 273?273 不是图像的宽度(182 是,我什至不知道这个数字来自哪里),但它是按钮实际显示的宽度。帮我解释一下。

4

1 回答 1

1

将其强制为 182 是错误的,但设置布局参数并没有错。480 宽度是 182 如果宽度变为 320 或说 720 你应该试试

private void getTheDisplay() {

        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        width = dm.widthPixels;
        hieght = dm.heightPixels;
    }



RelativeLayout.LayoutParams myButtonLayout = (RelativeLayout.LayoutParams) viewHolder.myButton.getLayoutParams();
myButtonLayout.width = ((182*width)/480);  //---got the ratio 182/480 and got the exact width for other screen size as well
myButtonLayout.height = ((182*height)/854);
viewHolder.myButton.setLayoutParams(myButtonLayout);

它会针对每个屏幕尺寸自动放大

在评论中回答您的问题

273 宽度来自像素到 dp 的转换这就是它的工作原理

您看到的尺寸=(图像的实际尺寸 * 您的屏幕像素密度)/160

在你的情况下是 (182*240)/160=273

于 2013-06-25T12:18:39.560 回答