[英]How can I draw 5x5 matrix of ImageButtons programmatically?
我需要ImageButton的高度和宽度相等,彼此之间没有空格,如下图所示,并且矩阵的开头和结尾不得有此类空格
非常感谢你! PS我在这里使用的代码是`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*"
android:layout_marginTop="-4.5dp"
android:paddingBottom="-5dp"
android:paddingLeft="-5dp"
android:paddingRight="-5dp"
android:paddingTop="-5dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageButton
android:layout_height="fill_parent"
android:id="@+id/ImageButton_11"
android:layout_width="0dp"
android:layout_weight="1"
/>
<ImageButton
android:id="@+id/ImageButton_12"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="-8.5dp"
android:layout_weight="1" />
<ImageButton
android:id="@+id/ImageButton_13"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_14"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_15"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp" >
<ImageButton
android:id="@+id/ImageButton_21"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/ImageButton_22"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_23"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_24"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_25"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp" >
<ImageButton
android:id="@+id/ImageButton_31"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/ImageButton_32"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_33"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_34"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_35"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
>
<ImageButton
android:id="@+id/ImageButton_41"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/ImageButton_42"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_43"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_44"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_45"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp" >
<ImageButton
android:id="@+id/ImageButton_51"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ImageButton
android:id="@+id/ImageButton_52"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_53"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_54"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
<ImageButton
android:id="@+id/ImageButton_55"
android:layout_width="0dp"
android:scaleType="centerCrop"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_marginLeft="-8.5dp"
/>
</TableRow>
</TableLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="@+id/refresh_ImageButton"
/>
</LinearLayout>
现在,我希望以编程方式完成它。
您的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
您的Java文件:
LinearLayout container = (LinearLayout) activity.findViewById(R.id.container);
TableLayout table = new TableLayout(activity);
LayoutParams matchWrap = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
LayoutParams zeroMatchOne = new TableRow.LayoutParams(
0, LayoutParams.MATCH_PARENT, 1f);
for (int i=0; i<5; i++) {
TableRow row = new TableRow(activity);
row.setLayoutParams(matchWrap);
for(int j=0; j<5; j++) {
ImageButton image = new ImageButton(activity);
image.setLayoutParams(zeroMatchOne);
row.addView(image);
}
table.addView(row);
}
如果您想在此处添加ID,则第二个答案应该可以!
如果要添加边距或填充:
layoutParams.setMargins(int left, int top, int right, int bottom);
view.setPadding(int left, int top, int right, int bottom);
我知道它看起来并不完全像您的样子,但是它应该使您知道如何做!
我的回答有点延迟(非常类似于上面的回答,很抱歉,但是我正在寻找另一种消除按钮边距的方法:)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainContainer = (LinearLayout)findViewById(R.id.main_container);
int cols = 5;
int rows = 5;
//Params used for cells
TableRow.LayoutParams params = new TableRow.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f
);
int pixelsToDp = convertToPixelsDp(-4.5f);
params.setMargins(pixelsToDp, pixelsToDp, pixelsToDp, pixelsToDp);
//Params used for rows
TableRow.LayoutParams rowParams = new TableRow.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
rowParams.setMargins(0, 0, 0, 0);
//Params used for table
TableLayout.LayoutParams tableParams = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT
);
tableParams.setMargins(0, 0, 0, 0);
//Table generation
TableLayout table = new TableLayout(this);
table.setLayoutParams(tableParams);
table.setShowDividers(TableLayout.SHOW_DIVIDER_NONE);
table.setStretchAllColumns(true);
for(int i = 0; i < rows; i++){
TableRow row = new TableRow(this);
row.setShowDividers(TableRow.SHOW_DIVIDER_NONE);
row.setLayoutParams(rowParams);
for(int j = 0; j < cols; j++){
ImageButton btn = new ImageButton(this);
btn.setPadding(0, 0, 0, 0);
btn.setLayoutParams(params);
btn.setScaleType(ImageView.ScaleType.CENTER_CROP);
//The button's background has the unneeded margin
//so if you set correct background drawable I think
//you might achieve what you want API lvl 16 above
//btn.setBackground(null);
//API Level 17 and above
//If you want id's for some reason you could store them in
//some kind of Dictionary or you could store the UI elements
//to have access to them
//int id = btn.generateViewId();
//btn.setId(id);
row.addView(btn);
}
table.addView(row);
}
mainContainer.addView(table);
}
public int convertToPixelsDp(float pixels){
int px = (int)TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
pixels,
this.getResources().getDisplayMetrics());
return px;
}
我建议您从基本布局类型(如FrameLayout或LinearLayout等)创建按钮,以对其进行更多控制这是我在我的项目中所做的事情:
这段代码使用不同的可绘制对象来定义按钮的状态,这些按钮只是普通的布局视图,如果需要,您可以在选择器中添加更多状态,但是我只使用了按下和普通按钮。
因此,对于选择器中定义的每个状态,都会使用另一个可绘制文件。
给定的示例是为我的按钮创建圆角边框并为其指定颜色
可绘制对象中的down_button_lesser_opacity_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Pressed -->
<item
android:state_pressed="true"
android:drawable="@drawable/down_button_lesser_opacity_red_pressed"
/>
<!-- Normal -->
<item
android:drawable="@drawable/down_button_lesser_opacity_red_normal"/>
</selector>
down_button_lesser_opacity_red_pressed.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="0dp" android:color="@color/transparent" />
<solid android:color="#55FF0000"/>
<corners
android:topLeftRadius="0dp" android:topRightRadius="0dp"
android:bottomLeftRadius="6dp" android:bottomRightRadius="6dp"
/>
<padding
android:left="6dp" android:top="0dp"
android:right="6dp" android:bottom="6dp"
/>
</shape>
down_button_lesser_opacity_red_normal.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="0dp" android:color="@color/transparent" />
<solid android:color="@color/feature_normal_item"/>
<corners
android:topLeftRadius="0dp" android:topRightRadius="0dp"
android:bottomLeftRadius="6dp" android:bottomRightRadius="6dp"
/>
<padding
android:left="6dp" android:top="0dp"
android:right="6dp" android:bottom="6dp"
/>
</shape>
你如何使用它:
android:background="@drawable/down_button_lesser_opacity_selector"
我第一次错过的简短补充内容以及一些包含可绘制对象的示例代码
生成时将其放在按钮的位置
FrameLayout btn = new FrameLayout(this);
btn.setPadding(0, 0, 0, 0);
btn.setLayoutParams(new TableRow.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT
));
TextView text = new TextView(this);
text.setText("Button: " + i + j);
btn.addView(text);
btn.setBackgroundResource(R.drawable.down_button_lesser_opacity_selector);
btn.setClickable(true);
class MyFrameLayout extends FrameLayout
{
MyFrameLayout(Context context){
super(context);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh){
Log.v("measure", "width:" + w + " height:" + h);
if(w != h) {
this.setLayoutParams(new TableRow.LayoutParams(
w, w
));
}
}
}
您可以通过更好的方式进行覆盖,使其更通用,但对于您的情况还是有效的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.