假设我有一个线性布局,我想在我的程序中,从Java代码中添加一个视图。使用什么方法?我问的不是如何在XML中完成它(我知道XML是如何完成的),而是如何沿着这个示例代码行完成一些事情?

(One View).add(Another View)

就像在Swing中一样。


LinearLayout是ViewGroup的子类,它有一个叫做addView的方法。addView方法应该是您想要的。


调用addView是正确的答案,但是要使它工作,还需要做更多的工作。

如果你通过构造函数创建一个视图(例如,Button myButton = new Button();),你需要在新构造的视图上调用setLayoutParams,在你将新构造的子视图添加到父视图之前,传入父视图的LayoutParams内部类的实例。

例如,你可能在你的onCreate()函数中有以下代码,假设你的LinearLayout有id R.id.main:

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参数。获得正确的内部类也很重要。我努力将视图添加到TableRow以正确显示,直到我发现我没有传递TableRow的实例。子视图的setLayoutParams。


要以编程方式添加视图,您可以执行以下操作:

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);

您还可以添加任意数量的视图。


我发现最好的方法是使用View的充气静态方法。

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)
}

从活动中添加视图的另一种方法

ViewGroup rootLayout = findViewById(android.R.id.content);
rootLayout.addView(view);

以编程方式设置约束的想法可能令人厌烦。下面的解决方案将适用于任何布局,无论是约束,线性等。最好的方法是设置一个占位符,即一个具有适当约束的FrameLayout(或在其他布局中适当放置,如线性),在你期望以编程方式创建视图的位置。

你所需要做的就是通过编程方式将视图膨胀,并使用addChild()方法将其作为FrameLayout的子视图。然后在运行时,您的视图将被膨胀并放置在正确的位置。根据Android的建议,你应该只添加一个childView到FrameLayout [link]。

下面是你的代码看起来像什么,假设你希望在一个特定的位置以编程方式创建TextView:

步骤1:

在你的布局中,它将包含要被膨胀的视图,在正确的位置放置一个FrameLayout,并给它一个id,比如“container”。

步骤2 创建一个布局,根元素作为你想在运行时膨胀的视图,调用布局文件为"textview.xml":

<?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>

顺便说一下,设置你的frameLayout的layout-params为wrap_content,否则框架布局将变得和父元素一样大,即活动,即手机屏幕。

android:layout_width="wrap_content"
android:layout_height="wrap_content"

如果没有设置,因为框架的子视图,默认情况下,会到框架布局的左上方,因此你的视图会简单地飞到屏幕的左上方。

步骤3

在你的onCreate方法中,这样做:

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来附加膨胀视图是相同的。更多信息,请看这个。)


你们也应该确保当你重写onLayout时,你必须调用super。onLayout的所有属性,否则视图将不会被膨胀!