gpt4 book ai didi

android - 以编程方式在 RelativeLayout 中设置 EditText 和 TextView 的位置

转载 作者:搜寻专家 更新时间:2023-11-01 09:09:46 24 4
gpt4 key购买 nike

我在 main.xml 中定义了一个 RelativeLayout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>

并在 onCreate 中以编程方式添加 TextView 和 EditText:

public void onCreate(Bundle savedInstanceState){
setContentView(R.layout.main);

addContentView(customGlView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));

addContentView(myTextView, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(myEditText, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
}

它们都出现了,但在左上角重叠了。我花了几个小时弄清楚如何将它们放在彼此的正下方,或者一个在屏幕的左下角,另一个在右上角。如果我通过 main.xml 添加它们,它们都不会出现。谁能给我一个正确方向的提示?

最佳答案

对于 RelativeLayout,您需要使用 LayoutParams 指定元素的相对位置:

myTextView.setId(1);
myEditText.setId(2);

addContentView(myTextView, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

final RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
params.addRule(RelativeLayout.BELOW, myTextView.getId());
addContentView(myEditText, params);

关于android - 以编程方式在 RelativeLayout 中设置 EditText 和 TextView 的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9094421/

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