首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >程序化RelativeLayout重叠

程序化RelativeLayout重叠
EN

Stack Overflow用户
提问于 2012-12-17 04:06:14
回答 1查看 1.6K关注 0票数 0

我在代码中实现相对布局时遇到了一个问题。我想要的是,TextView1位于屏幕的左侧,TextView2位于屏幕右侧的一行。

它在XML中工作得很好,但是在代码中做不到……可能的原因是什么?

这个工作很好:

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:text="@string/hello_world" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:text="TextView" />

</RelativeLayout>

这不起作用(给出了元素的重叠):

代码语言:javascript
复制
RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);

TextView t1 = new TextView(this);   
t1.setText("balbla_1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
t1.setLayoutParams(lp1);

TextView t2 = new TextView(this);
t2.setText("balbla_2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
t2.setLayoutParams(lp2);        

rel.addView(t1);
rel.addView(t2);

setContentView(rel);

但这将很好地工作:(用WRAP_CONTENT替换MATCH_PARENT ):

代码语言:javascript
复制
RelativeLayout rel = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
        LayoutParams.WRAP_CONTENT);
rel.setLayoutParams(params);

TextView t1 = new TextView(this);   
t1.setText("balbla_1");
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
t1.setLayoutParams(lp1);

TextView t2 = new TextView(this);
t2.setText("balbla_2");
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT,
        RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
t2.setLayoutParams(lp2);        

rel.addView(t1);
rel.addView(t2);

setContentView(rel);
EN

回答 1

Stack Overflow用户

发布于 2012-12-17 04:08:33

Java代码与XML并不完全匹配,重叠是因为您忘记了:

代码语言:javascript
复制
lp2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);

你也忽略了:

代码语言:javascript
复制
lp1.addRule(RelativeLayout.CENTER_VERTICAL);

并且您正在使用MATCH_PARENT而不是WRAP_CONTENT作为宽度。

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

https://stackoverflow.com/questions/13905023

复制
相关文章

相似问题

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