假设我有一个LinearLayout
,并且我想在Java代码中向其中添加一个视图。我应该使用哪种方法?我不是在问如何在XML中实现,这一点我已经知道了。我想知道的是,我如何实现下面这个示例代码中类似的操作:
(One View).add(Another View)
就像在Swing中可以做的那样。
假设我有一个LinearLayout
,并且我想在Java代码中向其中添加一个视图。我应该使用哪种方法?我不是在问如何在XML中实现,这一点我已经知道了。我想知道的是,我如何实现下面这个示例代码中类似的操作:
(One View).add(Another View)
就像在Swing中可以做的那样。
调用 addView
是正确的答案,但你需要做更多的工作才能让它正常工作。
如果你通过构造函数创建一个 View(例如,Button myButton = new Button();
),在将新创建的子 View 添加到父 View 之前,你需要调用 setLayoutParams
方法并传入父 View 的 LayoutParams 内部类的实例。
例如,假设你的 LinearLayout 具有 ID R.id.main
,则你的 onCreate()
函数中可能会有以下代码:
LinearLayout myLayout = findViewById(R.id.main);
Button myButton = new Button(this);
myButton.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
myLayout.addView(myButton);
设置LayoutParams很重要。每个视图都需要至少一个layout_width和layout_height参数。同时选择正确的内部类也很重要。我曾经遇到过将View添加到TableRow中无法正常显示的问题,直到我发现没有向子视图的setLayoutParams方法传递TableRow.LayoutParams实例。
我发现最好的方法是使用View的inflate静态方法。
View inflatedView = View.inflate(context, yourViewXML, yourLinearLayout);
您的yourViewXML类似于R.layout.myView。请注意,您需要一个ViewGroup才能添加视图(这可以是任何布局)。
因此,举个例子,假设您有一个片段,它的视图已经被填充,并且您知道根视图是一个布局,并且您希望向其中添加一个视图:
View view = getView(); // returns base view of the fragment
if (view == null)
return;
if (!(view instanceof ViewGroup))
return;
ViewGroup viewGroup = (ViewGroup) view;
View popup = View.inflate(viewGroup.getContext(), R.layout.someView, viewGroup);
编辑:
上述示例的 Kotlin 代码(view 是片段的 getView()):
(view as? ViewGroup)?.let {
View.inflate(context, R.layout.add_credit_card, it)
}
addView(View v){ // your code after get view }
,该方法将获取使用 findViewById(int resourceID)
找到的视图或像你的 inflatedView
一样被填充的视图...只是为了扩展你的片段示例。 - zgc7009myLinearLayout.add(view)
之后,这是唯一有效的方法。感谢您! - Eric B.要通过编程方式添加视图,您可以执行以下操作:
LinearLayout rlmain = new LinearLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.FILL_PARENT);
LinearLayout ll1 = new LinearLayout (this);
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.logo);
LinearLayout .LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
iv.setLayoutParams(lp);
ll1.addView(iv);
rlmain.addView(ll1);
setContentView(rlmain, llp);
你还可以添加任意数量的视图。addChild()
方法将视图以编程方式填充并将其作为子项添加到FrameLayout中。然后在运行时,您的视图将被填充并放置在正确的位置。根据Android的建议,您应该只向FrameLayout[链接]添加一个childView。<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
</TextView>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
FrameLayout frameLayout = findViewById(R.id.container);
TextView textView = (TextView) View.inflate(this, R.layout.textview, null);
frameLayout.addView(textView);
findViewById
的最后一个参数设置为null
,并通过在容器视图(frameLayout)上调用addView()
来添加视图,与仅通过在findViewById()
的第三个参数中传递true
来传递充气视图是相同的。有关更多信息,请参见this。)一种从Activity添加视图的方法
ViewGroup rootLayout = findViewById(android.R.id.content);
rootLayout.addView(view);
你们还需要确保在重写 onLayout
方法时,一定要调用 super.onLayout
并传入所有属性,否则视图将无法被填充!