c# - 如何在 C# 的 Android Xamarin 中设置按钮的布局宽度?

标签 c# android layout code-behind

我正在 Xamarin Android C# 项目中使用 Android 按钮。

<Button          
android:id="@+id/btn1"          
android:layout_width="120dp"          
android:layout_height="120dp"          
android:background="@drawable/ButtonImages"          
android:layout_marginLeft="35dp"   />     

在这里,我想从后面的代码中设置这个 Android: layout_width="120dp"Android: layout_marginLeft="35dp"

我从后面的代码中得到了按钮控件

Button btn1 = FindViewById<Button>(Resource.Id.btn1);

但无法设置widthma​​rginLeft

我该怎么做?

最佳答案

尚未测试这是 xamarin,但理想情况下,这应该可以工作,请尝试并更新。在这里,我已经设置了高度和重量为120dp,你可以根据自己的需要进行修改。也可以通过替换文本来根据需要设置边距。

Button btn1 = FindViewById<Button>(Resource.Id.btn1);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams (120,120);
params.setMargins(left, top, right, bottom);
btn1.setLayoutParams(params);

关于c# - 如何在 C# 的 Android Xamarin 中设置按钮的布局宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52255254/

相关文章:

android - 将 WTP 插件安装到 ADT eclipse 时出错

c# - C# 中的简单优先级队列 - 使用自定义排序器 : IComparer? 会比 List 更好

c# - 如何在单击 Xamarin 按钮时添加自定义 View

c# - 如何使用带有 Emgu CV 的图像处理将图像转换为二值图像

android - 如何在 onHandleIntent() 完成时不结束 Android IntentService?

android - 从顶部滑动 View 到另一个布局android

c# - 如何在 .NET 紧凑型框架中获取默认系统字体?

javascript - 如何注入(inject)javascript并修改原生安卓浏览器谷歌搜索结果

apache-flex - 具有用于分页的动态行和列的 TileLayout

model-view-controller - Zend Framework 2 : Modify details of layout. Controller 中的 pthml