RelativeLayout用代码实现布局

TextView txt1 = new TextView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);

params.addRule(RelativeLayout.LEFT_OF, 1001);
txt1.setLayoutParams(params);

TextView txt2 = new TextView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams. WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
txt2.setLayoutParams(params2);
txt2.setText("obj");
txt2.setId(1001);

RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout1.addView(txt1);
layout1.addView(txt2);
setContentView(layout1);


对应的XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:id="@+id/leftobj"
android:hint="left of the base"
android:layout_alignParentLeft="true"
android:layout_width="fill_parent"
android:layout_toLeftOf="@+id/base"
android:layout_height="wrap_content"></TextView>
<TextView
android:id="@+id/base"
android:text="obj"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_height="wrap_content"></TextView>
</RelativeLayout>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 RelativeLayout实现竖直布局,可以使用以下两种方法: 方法一:使用布局参数 在 RelativeLayout 中,可以使用 `android:layout_alignParentTop` 和 `android:layout_alignParentBottom` 属性来将视图定位在容器的顶部或底部,同时使用 `android:layout_below` 和 `android:layout_above` 属性来指定视图的上方或下方。结合这些属性,可以实现RelativeLayout 中的竖直布局。 例如,以下代码可以将两个 TextView 垂直居中: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text 1" android:layout_alignParentTop="true" android:layout_above="@+id/textView2" android:layout_centerHorizontal="true" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text 2" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` 在上面的代码中,我们将第一个 TextView 定位在容器的顶部,并将其上方与容器的顶部对齐,将其下方与第二个 TextView 上方对齐。同时,我们将第二个 TextView 定位在容器的底部,并将其上方与第一个 TextView 下方对齐。这样就可以实现两个 TextView 在 RelativeLayout 中的垂直居中布局。 方法二:使用 layout_centerVertical 属性 另一种方法是使用 `android:layout_centerVertical` 属性将视图垂直居中。此属性将视图垂直居中放置在 RelativeLayout 中。 例如,以下代码可以将一个 TextView 垂直居中: ``` <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Text" android:layout_centerVertical="true" android:layout_centerHorizontal="true" /> </RelativeLayout> ``` 在上面的代码中,我们将 TextView 定位在容器的中心,并将其垂直居中对齐。这样就可以实现一个 TextView 在 RelativeLayout 中的垂直居中布局
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值