android layout xml 传递参数,android - 如何在不在xml中的代码中设置RelativeLayout布局参数?...

android - 如何在不在xml中的代码中设置RelativeLayout布局参数?

例如,我想在屏幕上添加3个按钮:一个对齐左边,一个对齐中心,最后一个对齐右边。

如何在代码中设置其布局,而不是在xml中?

5个解决方案

266 votes

只是一个基本的例子:

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);

Button button1;

button1.setLayoutParams(params);

params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

params.addRule(RelativeLayout.RIGHT_OF, button1.getId());

Button button2;

button2.setLayoutParams(params);

如您所见,这是您必须做的:

创建一个addRule(int)对象。

使用addRule(int)或addRule(int, int)设置规则。 第一种方法用于添加不需要值的规则。

将参数设置为视图(在本例中为每个按钮)。

Cristian answered 2019-06-29T12:37:45Z

17 votes

RelativeLayout layout = new RelativeLayout(this);

RelativeLayout.LayoutParams labelLayoutParams = new RelativeLayout.LayoutParams(

LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);

layout.setLayoutParams(labelLayoutParams);

// If you want to add some controls in this Relative Layout

labelLayoutParams = new RelativeLayout.LayoutParams(

LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

labelLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

ImageView mImage = new ImageView(this);

mImage.setBackgroundResource(R.drawable.popupnew_bg);

layout.addView(mImage,labelLayoutParams);

setContentView(layout);

Amit Thaper answered 2019-06-29T12:38:02Z

6 votes

像这样的东西......

RelativeLayout linearLayout = (RelativeLayout) findViewById(R.id.widget43);

// ListView listView = (ListView) findViewById(R.id.ListView01);

LayoutInflater inflater = (LayoutInflater) this

.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

// View footer = inflater.inflate(R.layout.footer, null);

View footer = LayoutInflater.from(this).inflate(R.layout.footer,

null);

final RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.FILL_PARENT,

RelativeLayout.LayoutParams.FILL_PARENT);

layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);

footer.setLayoutParams(layoutParams);

Rohit Mandiwal answered 2019-06-29T12:38:27Z

4 votes

如果您创建它,只需从视图本身拉出布局参数即可。

$((RelativeLayout)findViewById(R.id.imageButton1)).getLayoutParams();

Chidi Michael Ekeocha answered 2019-06-29T12:38:53Z

0 votes

我希望下面的代码会有所帮助。 它将创建一个EditText和一个Log In按钮。 两者都相对而言。 全部在MainActivity.java中完成。

package com.example.atul.allison;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.RelativeLayout;

import android.widget.Button;

import android.graphics.Color;

import android.widget.EditText;

import android.content.res.Resources;

import android.util.TypedValue;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

//Layout

RelativeLayout atulsLayout = new RelativeLayout(this);

atulsLayout.setBackgroundColor(Color.GREEN);

//Button

Button redButton = new Button(this);

redButton.setText("Log In");

redButton.setBackgroundColor(Color.RED);

//Username input

EditText username = new EditText(this);

redButton.setId(1);

username.setId(2);

RelativeLayout.LayoutParams buttonDetails= new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT

);

RelativeLayout.LayoutParams usernameDetails= new RelativeLayout.LayoutParams(

RelativeLayout.LayoutParams.WRAP_CONTENT,

RelativeLayout.LayoutParams.WRAP_CONTENT

);

//give rules to position widgets

usernameDetails.addRule(RelativeLayout.ABOVE,redButton.getId());

usernameDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

usernameDetails.setMargins(0,0,0,50);

buttonDetails.addRule(RelativeLayout.CENTER_HORIZONTAL);

buttonDetails.addRule(RelativeLayout.CENTER_VERTICAL);

Resources r = getResources();

int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200,r.getDisplayMetrics());

username.setWidth(px);

//Add widget to layout(button is now a child of layout)

atulsLayout.addView(redButton,buttonDetails);

atulsLayout.addView(username,usernameDetails);

//Set these activities content/display to this view

setContentView(atulsLayout);

}

}

Atul Chavan answered 2019-06-29T12:39:19Z

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值