我需要ImageButtons具有相同的高度和宽度,彼此之间没有空格,如下图所示,并且在矩阵
非常感谢! 附:我在这里使用的代码是`
<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>
现在我希望它以编程方式完成。
答案 0 :(得分:1)
您的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外观here,第二个答案就应该这样做!
如果要添加边距或填充:
layoutParams.setMargins(int left, int top, int right, int bottom);
view.setPadding(int left, int top, int right, int bottom);
我知道它看起来并不像你的,但它应该让你知道如何做到这一点!
答案 1 :(得分:1)
稍微延迟我的答案(非常类似于上面的那个,所以我很抱歉,但正在寻找另一种方法来删除按钮的边缘:)
@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等......以便更好地控制它们这就是我在我的项目中所做的:
此代码使用不同的drawable来定义我的按钮的状态,这些只是普通的布局视图,如果需要,你可以在选择器中添加更多的状态,但是我只使用了按下和正常。
因此,对于选择器中定义的每个状态,使用另一个可绘制文件。
给出的示例是为我的按钮创建圆形边框并为其指定颜色
drawables中的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"
我第一次错过的简短补充和一些示例代码包括drawables
生成
时将其放在按钮的位置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
));
}
}
}
您可以更好地执行覆盖,使其更通用,但对于您的情况,