如何以编程方式设置权重按钮 [英] How to programmatically set weight for Button

查看:128
本文介绍了如何以编程方式设置权重按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要实现对话我通过Java code Android应用程序,所以我不能使用XML。

我有根线的布局,我执行范围搜索栏,那么我还有一个线性布局下根布局,水平方向,在这里我想补充两个按钮在同一行。所以,我需要设置权重为1,宽度为FILL_PARENT和高度WRAP_PARENT。

我如何能做到这一点与Java code?

解决方案

  LinearLayout.LayoutParams P =新LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;

rangeSeekBar.setLayoutParams(对);
 

我不知道哪个视图要设置布局PARAMS上。我刚刚就任rangeSeekbar显示一个例子。如果你需要更改。

在使用的布局PARAMS始终使用root的参数类型。

例。如果你有一个查看你想在一个 RelativeLayout的使用 RelativeLayout.LayoutParams PARAMS申请 ..

I need to implement Dialog for my Android app through Java code, so I can't use XML.

I have root Linear layout where I implement range seek bar, then I have another Linear Layout under root layout, with horizontal orientation, where I want to add two buttons in same row. So I need to set weight to 1, and width to FILL_PARENT and height to WRAP_PARENT.

How I can do that with Java code?

解决方案

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1;

rangeSeekBar.setLayoutParams(p);

I'm not sure which view you want to set the layout params on. I just assumed the rangeSeekbar to show an example. Change if you need.

When using the layout params always use the root's param type..

Ex. if you have a View you want to apply params to within a RelativeLayout use RelativeLayout.LayoutParams..

这篇关于如何以编程方式设置权重按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆