C# TableLayout中的Imagebutton太大
我有一个滚动视图,里面有一个表格布局。Tablelayout在xml中定义如下:C# TableLayout中的Imagebutton太大,c#,android,xamarin,layout,imageview,C#,Android,Xamarin,Layout,Imageview,我有一个滚动视图,里面有一个表格布局。Tablelayout在xml中定义如下: <TableLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/tablelayout"/> private void createFeedLayout(int menuId, int ne
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tablelayout"/>
private void createFeedLayout(int menuId, int newestOrBest, int count, int startId) {
TableLayout tbl = (TableLayout)FindViewById(Resource.Id.tablelayout);
List<Helpers.Objects.Picture> pictures = getPictures(menuId, newestOrBest, startId, count);
TableRow row = new TableRow(this);
tbl.AddView(row);
row.SetBackgroundResource(Resource.Drawable.background_whitebar_challengeopen);
foreach (Helpers.Objects.Picture picture in pictures)
{
if (counter == PICTURESINAROW)
{
row = new TableRow(this);
row.SetGravity(GravityFlags.Left);
row.SetBackgroundResource(Resource.Drawable.background_whitebar_challengeopen);
tbl.AddView(row);
counter = 0;
}
Bitmap bmp_picture = PhotoHelpers.DecodePhotoFromBase64(picture.photo);
bmp_picture = PhotoHelpers.ResizeImage(bmp_picture, (Resources.DisplayMetrics.WidthPixels / PICTURESINAROW) - SPACEBETWEENPICS,
(Resources.DisplayMetrics.WidthPixels / PICTURESINAROW) - SPACEBETWEENPICS, false);
ImageButton button = new ImageButton(this);
button.Id = i;
button.Click += openImage;
button.SetImageBitmap(bmp_picture);
button.SetBackgroundColor(Color.Red);
button.SetAdjustViewBounds(true);
row.AddView(button);
counter++;
i++;
}
}
tablelayout的行及其视图按如下编程方式添加:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tablelayout"/>
private void createFeedLayout(int menuId, int newestOrBest, int count, int startId) {
TableLayout tbl = (TableLayout)FindViewById(Resource.Id.tablelayout);
List<Helpers.Objects.Picture> pictures = getPictures(menuId, newestOrBest, startId, count);
TableRow row = new TableRow(this);
tbl.AddView(row);
row.SetBackgroundResource(Resource.Drawable.background_whitebar_challengeopen);
foreach (Helpers.Objects.Picture picture in pictures)
{
if (counter == PICTURESINAROW)
{
row = new TableRow(this);
row.SetGravity(GravityFlags.Left);
row.SetBackgroundResource(Resource.Drawable.background_whitebar_challengeopen);
tbl.AddView(row);
counter = 0;
}
Bitmap bmp_picture = PhotoHelpers.DecodePhotoFromBase64(picture.photo);
bmp_picture = PhotoHelpers.ResizeImage(bmp_picture, (Resources.DisplayMetrics.WidthPixels / PICTURESINAROW) - SPACEBETWEENPICS,
(Resources.DisplayMetrics.WidthPixels / PICTURESINAROW) - SPACEBETWEENPICS, false);
ImageButton button = new ImageButton(this);
button.Id = i;
button.Click += openImage;
button.SetImageBitmap(bmp_picture);
button.SetBackgroundColor(Color.Red);
button.SetAdjustViewBounds(true);
row.AddView(button);
counter++;
i++;
}
}
private void createFeedLayout(int-menuId、int-newestOrBest、int-count、int-startId){
TableLayout tbl=(TableLayout)findviewbyd(Resource.Id.TableLayout);
列表图片=获取图片(menuId、newestOrBest、startId、count);
TableRow row=新的TableRow(本);
tbl.AddView(行);
行.SetBackgroundResource(Resource.Drawable.background\u whitebar\u challengeopen);
foreach(图片中的Helpers.Objects.Picture)
{
如果(计数器==PICTURESINAROW)
{
行=新表行(此);
行设置重力(重力梯度左);
行.SetBackgroundResource(Resource.Drawable.background\u whitebar\u challengeopen);
tbl.AddView(行);
计数器=0;
}
位图bmp_picture=PhotoHelpers.DecodePhotoFromBase64(picture.photo);
bmp_picture=PhotoHelpers.ResizeImage(bmp_picture,(Resources.DisplayMetrics.WidthPixels/picturesInRow)-SPACEBETWEENPICS,
(Resources.DisplayMetrics.WidthPixels/PicturesInRow)-SPACEBETWEENPICS,false);
ImageButton按钮=新的ImageButton(此按钮);
按钮Id=i;
按钮。单击+=打开图像;
按钮。设置图像位图(bmp_图片);
按钮。SetBackgroundColor(颜色。红色);
按钮。SetAdjustViewBounds(真);
行。添加视图(按钮);
计数器++;
i++;
}
}
}
该代码在Tablerow中添加3张图片,然后创建新行。预期结果将是3张图片,从屏幕的左边缘开始,彼此紧邻
但我得到的是这样的东西:
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/tablelayout"/>
private void createFeedLayout(int menuId, int newestOrBest, int count, int startId) {
TableLayout tbl = (TableLayout)FindViewById(Resource.Id.tablelayout);
List<Helpers.Objects.Picture> pictures = getPictures(menuId, newestOrBest, startId, count);
TableRow row = new TableRow(this);
tbl.AddView(row);
row.SetBackgroundResource(Resource.Drawable.background_whitebar_challengeopen);
foreach (Helpers.Objects.Picture picture in pictures)
{
if (counter == PICTURESINAROW)
{
row = new TableRow(this);
row.SetGravity(GravityFlags.Left);
row.SetBackgroundResource(Resource.Drawable.background_whitebar_challengeopen);
tbl.AddView(row);
counter = 0;
}
Bitmap bmp_picture = PhotoHelpers.DecodePhotoFromBase64(picture.photo);
bmp_picture = PhotoHelpers.ResizeImage(bmp_picture, (Resources.DisplayMetrics.WidthPixels / PICTURESINAROW) - SPACEBETWEENPICS,
(Resources.DisplayMetrics.WidthPixels / PICTURESINAROW) - SPACEBETWEENPICS, false);
ImageButton button = new ImageButton(this);
button.Id = i;
button.Click += openImage;
button.SetImageBitmap(bmp_picture);
button.SetBackgroundColor(Color.Red);
button.SetAdjustViewBounds(true);
row.AddView(button);
counter++;
i++;
}
}
(仅红色背景的部分)
在这里,我感到困惑。我添加了SetAdjustViewBounds(true)代码>到每个按钮,并认为这将导致按钮与图片一样大。因此,SetBackgroundColor(Color.Red)代码>根本不重要。但看起来按钮周围有一个大框架
有人知道是什么导致了这种行为吗
提前谢谢 如果希望图像按钮包装内容,则需要设置LayoutParameters。试试这个:
button.LayoutParameters = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WrapContent, ViewGroup.LayoutParams.WrapContent);
好吧,我开始工作了。问题在于桌面布局本身。屏幕中间的圆形图片也是这个版图的一部分。“红色”区域中的TableRows以某种方式调整了以前这些TableRows的列宽
因此,我创建了一个新的TableLayout,并为底部的图片添加了TableRows
我相信有一个更好的解决方案,但至少它现在起作用了 使用button.Background方法代替button.SetImageBitmap(bmp\U图片)方法:button.Background=新的BitmapDrawable(参考资料,bmp\U图片);你好我试过了,但现在这幅画只会被弄脏。所以它不再是正方形了。对我来说,似乎Tablelayout对于tablerow中的每一列都有一个静态宽度,因此按钮被拉伸以满足要求。可能吗?tablerow中每列的宽度由您自己决定,位图的宽度。您好,谢谢您的回答。我已经试过了,但没有任何改变。令人困惑的是。如果我通过按钮.LayoutParameters.width
获取按钮的宽度,则大小似乎是正确的。但它周围仍然有红色的边界。。。