English 中文(简体)
如何在方案上添加意见
原标题:How to Programmatically Add Views to Views
  • 时间:2010-03-07 09:31:45
  •  标签:
  • android

假设我有一个LinearLayout,并且我想在我的程序中从Java代码向其添加一个View。用于此的方法是什么?我不是在问如何在XML中完成它,因为我知道,而是如何执行类似于此示例代码的操作?

(One View).add(Another View)

和大家一样,在航海也是这样。

问题回答

调用addView是正确的答案,但你需要做更多的工作才能让它工作。

如果你通过构造函数创建一个视图(例如,Button myButton = new Button();),你需要在将你新构建的子视图添加到父视图之前,调用setLayoutParams方法,在其中传递父视图的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参数。同时,选择正确的内部类也很重要。在我弄清楚我没有传递TableRow.LayoutParams实例给子视图的setLayoutParams之前,我一直苦苦挣扎地让视图添加到TableRow中正确展示。

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

View inflatedView = View.inflate(context, yourViewXML, yourLinearLayout);

你的ViewXML类似于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 是一个 fragment 的 getView())

(view as? ViewGroup)?.let {
        View.inflate(context, R.layout.add_credit_card, it)
}

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

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

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

rel=“noreferer”>LinearLayout View,其方法为addView。 http://developer.android.com/fer/android/view/Viewgroup.html#addView(android.view)。 观点”“字体” 方法应该是你们的。

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

你需要做的就是通过编程实现可充气视图,并使用 addChild()方法将其作为子级添加到FrameLayout中。然后在运行时,您的视图将被充气并放置在正确的位置。根据Android的建议,您应该只向FrameLayout添加一个子视图[链接]

假设你希望以编程方式在特定位置创建TextView,以下是你的代码示例:

步骤1:

在包含需要填充的视图的布局中,放置一个位于正确位置的 FrameLayout,给它一个ID,比如说“container”。

Step 2 Create a layout with root element as the view you want to inflate during runtime, call the layout file as "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>

顺便说一下,将你的帧布局的布局参数始终设为“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来简单地附加充气视图相同。 更多信息,请参见此链接

还有一种方法可以从 Activity 中添加视图

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

你们还需要确保在覆盖onLayout时要将所有属性都与super.onLayout一起调用,否则视图将不会被膨胀!





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签