gpt4 book ai didi

java - relativelayout 在另外两个 View 之间插入一个 View

转载 作者:行者123 更新时间:2023-12-01 23:24:08 25 4
gpt4 key购买 nike

假设我在 RelativeLayout 中有两个按钮。顶部标有“一”的按钮,“一”下方标有“三”的按钮。布局是这样定义的。

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:id="@+id/mainContainer"
tools:context=".MainActivity" >

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvOne"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="One" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvThree"
android:layout_centerHorizontal="true"
android:layout_below="@id/tvOne"
android:text="Three" />
</RelativeLayout>

所以我在MainActivityonCreate中编写了一些代码来动态创建一个Button,并将其插入到一到三之间。但它不起作用。我有什么遗漏的吗?我创建这个问题是作为我遇到的一个更大问题的简化版本,所以我无法接受清除布局并仅动态插入一二三。

protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button one = (Button)findViewById(R.id.tvOne);
Button three = (Button)findViewById(R.id.tvThree);

//Dynamically create a button, set it underneath one and above two.
Button two = new Button(this);
two.setText("TWO TWO TWO TWO TWO TWO TWO");

//Create some layout params so that this button is horizontally centered,
//above button number three and below button number one
final int WC = RelativeLayout.LayoutParams.WRAP_CONTENT;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(WC, WC);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, one.getId());
params.addRule(RelativeLayout.ABOVE, three.getId());

two.setLayoutParams(params);

//Add button number two to the activity.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.mainContainer);
rl.addView(two);
}

最佳答案

工作代码,我已经检查过了。

public class Main extends Activity {

Context ctx;
RelativeLayout rlayMainContainer;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ctx = this;

rlayMainContainer = (RelativeLayout) findViewById(R.id.mainContainer);
Button one = (Button) findViewById(R.id.tvOne);
Button three = (Button) findViewById(R.id.tvThree);


// adding button two dynamically
Button two = new Button(ctx);
two.setText("hello");
two.setId(12);

RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpSecond.addRule(RelativeLayout.CENTER_HORIZONTAL);
lpSecond.addRule(RelativeLayout.BELOW, one.getId());

rlayMainContainer.addView(two, lpSecond);

//align button three below button two

RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) three
.getLayoutParams();

params.addRule(RelativeLayout.BELOW, two.getId());

three.setLayoutParams(params);
}
}

enter image description here

关于java - relativelayout 在另外两个 View 之间插入一个 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20260126/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com