gpt4 book ai didi

java - 如何以编程方式正确设置RelativeLayout的设置?

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

我正在以编程方式创建一个 RelativeLayout,并且我想将三个对象放入其中:一个 ListView 和两个 Button。我需要设置一些设置才能在窗口中正确找到它们。现在我这样做:

RekativeLayout rl = new RelativeLayout(context);
rl.setBackgroundColor(Color.WHITE);
rl.setPadding(10, 10, 10, 10);

ListView listView = new ListView(context);
ListViewAdapter adapter = new ListViewAdapter(jParser.getArrayList(), context);
listView.setLayoutParams(new LayoutParams(wWidth, wHeight));
listView.setAdapter(adapter);

RelativeLayout.LayoutParams listParams = new RelativeLayout.LayoutParams(wWidth, wHeight);
listParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
listParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
listView.setId(98515);
listView.setLayoutParams(listParams);

Button moreButton = new Button(context);
moreButton.setLayoutParams(lp);
moreButton.setBackgroundColor(Color.GRAY);
moreButton.setText("More");

RelativeLayout.LayoutParams mButtonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
mButtonParams.addRule(RelativeLayout.ALIGN_LEFT, listView.getId());
mButtonParams.addRule(RelativeLayout.BELOW, listView.getId());
mButtonParams.setMargins(0, 10, 0, 0);
moreButton.setLayoutParams(mButtonParams);

closeButton.setLayoutParams(lp);
closeButton.setBackgroundColor(Color.GRAY);
closeButton.setText("Close");

Button closeButton = new Button(context);
RelativeLayout.LayoutParams cButtonParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cButtonParams.addRule(RelativeLayout.ALIGN_RIGHT, listView.getId());
cButtonParams.addRule(RelativeLayout.BELOW, listView.getId());
cButtonParams.setMargins(0, 10, 0, 0);
closeButton.setLayoutParams(cButtonParams);

rl.addView(listView);
rl.addView(moreButton);
rl.addView(closeButton);

但我猜这种方法需要大量的系统资源, Not Acceptable 。我应该如何正确设置?

最佳答案

考虑到您提出的问题,您所做的一切都很好。为了这个问题,我仍然会回答这两个问题。
我想你的意思是 RelativeLayout.LayoutParams 。它允许您设置任何属性并自定义 View 的行为方式。

例如,创建和设置基本高度/宽度:

RelativeLayout myRelativeLayout = new RelativeLayout(this);
myRelativeLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

================================================== ============
现在,就您的其他疑问而言,您真正的问题是这是否是最佳的以及是否还有其他方法。 (虽然你确实应该提出另一个问题,但我仍然会回答)。正如评论中提到的,这是xml资源的主要原因。您可以在此处创建布局或 View ,然后用数据填充它们。

以下是快速指南:http://developer.android.com/guide/topics/ui/declaring-layout.html

快速分割:
-res/layout/是布局 xml 所在的目录。 ( LayoutInflater )
-在这些 View 中,您可以声明其他 View (TextView、ImageView 等)。
-您必须引用并扩充资源才能在代码中调用它们。
- 一旦膨胀,您可以调用 findViewById(resourceId) 并将 View 转换为正确的 Widget( View )类型。

膨胀和引用 View 项需要 Context 。以下是了解其工作原理的快速指南:
http://www.doubleencore.com/2013/06/context/

其他引用:您可能想更深入地了解 resources .

希望这对您有所帮助,祝您编码愉快!

关于java - 如何以编程方式正确设置RelativeLayout的设置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23227449/

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