首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在代码中访问weight属性

在代码中访问weight属性
EN

Stack Overflow用户
提问于 2013-01-10 06:17:14
回答 2查看 38关注 0票数 0

我的布局xml文件中有以下代码

代码语言:javascript
运行
复制
 <RelativeLayout
    android:id="@+id/contentTextViewLayout"
    android:layout_weight="9"
    android:layout_width="fill_parent"
    android:layout_height="0dip">
    <TextView
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</RelativeLayout>

如何在代码中获取RelativeLayout的layout_weight属性值?

EN

回答 2

Stack Overflow用户

发布于 2013-01-10 06:27:47

为什么不直接获取相对布局的宽度,然后将edittext的宽度设置为布局宽度的百分比。

代码语言:javascript
运行
复制
int i = relativelayout.getWidth();
int x = //any number, depending on how wide you what your text view
textview.setWidth(i/x);
票数 0
EN

Stack Overflow用户

发布于 2013-01-10 06:31:46

您必须使用RelativeLayout.LayoutParams

尝试以下操作:

代码语言:javascript
运行
复制
RelativeLayout r = ((RelativeLayout)(findViewById(R.id.contentTextViewLayout))); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
r.setLayoutParams(params);

params.weight = 1.0f可根据您的需要进行更改。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14247513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档