Android 在布局中动态添加滚动视图,在滚动视图中动态添加线性布局

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11789777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:52:30  来源:igfitidea点击:

Dynamically add scrollview in layout and linearlayout in scrollview

androidscrollviewandroid-linearlayout

提问by SleepNot

How do I dynamically add a scrollview in my layout? and once I get that scrollview added I want to add a linearlayout inside it so I can now add controls in that linearlayout?

如何在布局中动态添加滚动视图?一旦我添加了滚动视图,我想在其中添加一个线性布局,以便我现在可以在该线性布局中添加控件?

回答by Balaji

I hope it will helpful to you.

我希望它会对你有所帮助。

Try this Code...

试试这个代码...

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);

        ScrollView sv = new ScrollView(this);
        sv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
        ll.setOrientation(1);
        sv.addView(ll);

        for(int i = 0; i < 20; i++) 
        {
            Button b = new Button(this);
            b.setText("Button "+i);
            ll.addView(b);
        }

        rl.addView(sv);


        /* If you want to set entire layout as dynamically, then remove below lines in program :
         * setContentView(R.layout.activity_main);
         * RelativeLayout rl=(RelativeLayout)findViewById(R.id.rl);
         * rl.addView(sv);
         * 
         * And Add below line :
         * this.setContentView(sv);

        */

    }

回答by nam_ph

try this it will work :

试试这个它会起作用:

    TextView tv = new TextView(this);
    tv.setText(msg);       
    tv.setMovementMethod(new ScrollingMovementMethod());
    setContentView(tv);

回答by Yash

You can wrap your widget inside a ScrollView. Here's a simple example:

您可以将小部件包装在 ScrollView 中。这是一个简单的例子:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/txt"/>
    </LinearLayout>
</ScrollView>

If you want to do it in code:

如果你想在代码中做到这一点:

ScrollView sv = new ScrollView(this);
//Add your widget as a child of the ScrollView.
sv.addView(wView);