[英]How to set ImageButton size and when button is pressed show it on added image
这是我用来创建ImageButton的代码。 我所有的按钮都会动态创建。
//It is button which inherits from ImageView
ImageButton button = new ImageButton(this);
Drawable testPic = getResources().getDrawable(R.drawable.test_pic);
//button.setBackgroundColor(R.color.transparent_background);//transparent image button button background
//button.setImageDrawable( testPic );
button.setBackgroundDrawable(testPic);
//button.setMaxWidth(20);
button.setOnClickListener(mCorkyListener);
button.setTag(i);
//button.setId(i);
//Controls how the image should be resized or moved to match the size of this ImageView.
button.setScaleType( ScaleType.CENTER_INSIDE );
System.out.println("button with "+button.getMeasuredWidth());
System.out.println("button height "+button.getMeasuredHeight());
首先,我的System.out返回button with 0 and button height 0
但是在设备上,我看到它比我想要的要大,我将此按钮放入Scrollview:
LinearLayout pubLayout = (LinearLayout)findViewById( R.id.myFilesScrollerLayout);
pubLayout.addView( button );
<ScrollView
android:id="@+id/myFilesScroller"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
>
<LinearLayout
android:id="@+id/myFilesScrollerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
因此,如何根据ScrollView的大小更改ImageButton的大小。 另外如何显示该按钮被按下?
谢谢。
尝试这个
Drawable dr = getResources()。getDrawable(R.drawable.somedrawable); 位图位图=(((BitmapDrawable)dr).getBitmap(); //假设您要设置大小为50x50 Drawable d = new BitmapDrawable(Bitmap.createScaledBitmap(bitmap,50,50,true));
您也可以使用drawable.setBounds(0,0,50,50);。 但是在某些情况下,它可能不适用于某些图像视图。 尝试同时使用两者。
要检查按钮是否被按下,请使用xml中的选择器和项目。 此链接可能有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.