【问题标题】:how to add multiple textboxes in a single scrollbar programatically如何以编程方式在单个滚动条中添加多个文本框
【发布时间】:2014-05-10 09:45:55
【问题描述】:

我有以下代码问题是当我运行这个时所有文本框都没有显示在屏幕上所以我想我需要在滚动视图中添加所有文本框但我不知道如何。我也可以使用列表视图来做到这一点但我必须通过在 java 代码中以编程方式添加滚动视图来帮助任何人

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String[] textArray={"one","two","asdasasdf", "asdf" ,"dsdaa","fsvs","sd"};
    int length=textArray.length;

    LinearLayout layout = new LinearLayout(this);
      setContentView(layout);

    layout.setOrientation(LinearLayout.VERTICAL);        
    for(int i=0;i<length;i++)
    {
        TextView tv=new TextView(getApplicationContext());
        tv.setText(textArray[i]);
        tv.setTextSize(40);
        tv.setTextColor(Color.BLACK);
        tv.setPadding(20, 50, 20, 50);
        tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
        layout.addView(tv);
        //tv.setMovementMethod(new ScrollingMovementMethod());


    }
         }
     }

【问题讨论】:

  • 请发布您的 xml 代码

标签: android android-layout textbox scrollbar scrollview


【解决方案1】:
// try this way,hope this will help you...

@Override
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String[] textArray={"one","two","asdasasdf", "asdf" ,"dsdaa","fsvs","sd"};
        int length=textArray.length;

        ScrollView scrollView = new ScrollView(this);
        LinearLayout layout = new LinearLayout(this);

        layout.setOrientation(LinearLayout.VERTICAL);
        for(int i=0;i<length;i++)
        {
            TextView tv=new TextView(getApplicationContext());
            tv.setText(textArray[i]);
            tv.setTextSize(40);
            tv.setTextColor(Color.WHITE);
            tv.setPadding(20, 50, 20, 50);
            tv.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
            layout.addView(tv);
            //tv.setMovementMethod(new ScrollingMovementMethod());


        }
        scrollView.addView(layout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        setContentView(scrollView);

}

【讨论】:

    【解决方案2】:

    在xml文件的ScrollView下定义一个LinearLayout。给id字段给LinearLayout

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

    在您的班级中映射LinearLayout

    LinearLayout layout = (LinearLayout) findViewById(R.id.<ID>);
    

    在此处动态添加TextBox

    TextView textBox = new TextView(context);
    layout.addView(textBox);
    

    【讨论】:

      猜你喜欢
      • 2013-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多