这是我用来创建 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 大小。另外如何显示该按钮被按下?
谢谢。