android 如何动态设置控件的宽度和高度

 我来答
babyAn涩
推荐于2017-09-21 · 知道合伙人金融证券行家
babyAn涩
知道合伙人金融证券行家
采纳数:7678 获赞数:132044
2014年于上海市商贸旅游学校毕业,专业会计。后就读于上海市同济大学网络教育学院。

向TA提问 私信TA
展开全部
一、方法
  使用getLayoutParams() 和setLayoutParams()方法
二、示例代码
  LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams) aaa.getLayoutParams();
   // 取控件aaa当前的布局参数
  linearParams.height = 365; // 当控件的高强制设成365象素
  aaa.setLayoutParams(linearParams); // 使设置好的布局参数应用到控件aaa
三、原理
  a)getLayoutParams()和setLayoutParams()都是控件基类view的public方法,在外部也可以直接调用。
  b)由于LayoutParams一般是在加入容器中设置的,所以容易混淆所指定的布局属性究竟是保存在容器中,还是控件本身的属性,答案是控件本身。但是在设置时还是要注意布局属性与容器种类密切相关。
猴儇馁3
推荐于2017-10-01 · 超过63用户采纳过TA的回答
知道答主
回答量:124
采纳率:0%
帮助的人:60.3万
展开全部
android中的控件如果在xml布局文件中把控件的layout_width和layout_height写成固定值了,好像就不能再在程序中更改该控件的高度和宽度了,不知哪位大侠有何良策可以指教一二,如 xml文件内容如下: <LinearLayout android:id="@id/dialog_bottom_neutral" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1.0" android:gravity="center"<Buttonandroid:id="@id/dialog_bottom_neutral_button" android:layout_width="80.0dip" android:layout_height="28.0dip" android:background="@drawable/dlg_button" android:gravity="center" android:singleLine="true" android:text="mid" android:textColor="@drawable/dlg_button_text_color" android:textSize="14.0sp" / 小弟在程序中使用button.width和button.height设置没用,用LayoutParmas设置也没用
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
zr860525
2015-06-03 · TA获得超过181个赞
知道小有建树答主
回答量:234
采纳率:0%
帮助的人:121万
展开全部
android中的控件如果在xml布局文件中把控件的layout_width和layout_height写成固定值了,好像就不能再在程序中更改该控件的高度和宽度了,不知哪位大侠有何良策可以指教一二,如

xml文件内容如下:

<LinearLayout

android:id="@id/dialog_bottom_neutral"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1.0"

android:gravity="center" >

<Button

android:id="@id/dialog_bottom_neutral_button"

android:layout_width="80.0dip"

android:layout_height="28.0dip"

android:background="@drawable/dlg_button"

android:gravity="center"

android:singleLine="true"

android:text="mid"

android:textColor="@drawable/dlg_button_text_color"

android:textSize="14.0sp" />

</LinearLayout>

如何在程序中动态设置button的宽度和高度呢

小弟在程序中使用button.width和button.height设置没用,用LayoutParmas设置也没用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式