如何动态地添加元素与XML创建的视图视图、元素、动态、XML

由网友(黎明有星辰)分享简介:我想动态内容添加到已创建的XML视图。I'm trying to add dynamic content to a view that has been created with XML.我有一个观点profile_list有我喜欢的一些元素添加到滚动型。I have a view "profile_list"...

我想动态内容添加到已创建的XML视图。

I'm trying to add dynamic content to a view that has been created with XML.

我有一个观点profile_list有我喜欢的一些元素添加到滚动型。

I have a view "profile_list" that has a ScrollView that I like to add some elements to.

下面是我想要做一些code。

Here is some code of what I'm trying to do.

// Find the ScrollView 
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);

// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);

// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);

// Add the LinearLayout element to the ScrollView
sv.addView(ll);

// Display the view
setContentView(R.layout.profile_list);

该计划是添加一个TableLayout和动态,而不是填补它只是一个虚拟的文本,但首先我必须得到这个工作。

The plan is to add a TableLayout and fill it dynamically and not just a dummy text but first I must get this to work.

任何帮助是值得欢迎的。

Any help is welcome.

亲切的问候 欧莱

愚蠢的我,我留下了一个元素,我在我的XML文件滚动型!

Stupid me I had left a element in my ScrollView in my XML-file!

反正她是一个工作的例子:

Anyway her is a working example:

LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = inflater.inflate(R.layout.profile_list, null);

    // Find the ScrollView 
    ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);

    // Create a LinearLayout element
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    // Add text
    TextView tv = new TextView(this);
    tv.setText("my text");
    ll.addView(tv);

    // Add the LinearLayout element to the ScrollView
    sv.addView(ll);

    // Display the view
    setContentView(v);

和XML文件,以匹配

And the XML-file to match

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

    <ScrollView 
        android:id="@+id/scrollView1" 
        android:clickable="true" 
        android:layout_weight="1" 
        android:layout_width="fill_parent" 
        android:layout_marginBottom="50px" 
        android:layout_height="fill_parent">
    </ScrollView>

</LinearLayout>

希望这会帮助别人。感谢所有帮助!

Hope this will help someone. Thanks for all help!

推荐答案

这是您要添加的东西到视图的方式基本上是正确的 - 你刚刚得到的容器对象通过其ID,然后添加到

The way that you are adding things to the view is basically right - you just get the container object by its ID and then add them.

看起来你要设置的内容视图是错误的观点,但。你应该膨胀的观点,加上孩子,并将其设置为内容来看,还是从设置资源ID的内容视图然后做处理。你在做什么是操纵视图,然后添加不同的一个。尝试将你的setContentView(...)到块的开始。

It looks like you are setting the content view to be the wrong view though. You should inflate the view, add the children and set it as the content view, or set the content view from a resource ID then do the manipulation. What you are doing is manipulating a view and then adding a different one. Try moving your setContentView(...) to the start of the block.

阅读全文

相关推荐

最新文章