【问题标题】:Programmatically created views in ScrollView在 ScrollView 中以编程方式创建的视图
【发布时间】:2014-03-27 04:05:01
【问题描述】:

我想将以编程方式创建的视图添加到 ScrollView 中,并且 ScrollView 在 XML 文件中声明。如何做到这一点?

package com.example.multipleviews;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;

public class MainView extends Activity {

    LinearLayout main, child;
    ScrollView sc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_view);
        main = (LinearLayout) findViewById(R.id.main);
        // Toast.makeText(getApplicationContext(), "hello", 0).show();
        view();
    }

    private void view() {
        // TODO Auto-generated method stub
        main.removeAllViews();

        sc = (ScrollView) findViewById(R.id.sc);
        sc.removeAllViews();

        child = (LinearLayout) findViewById(R.id.lin);
        child.removeAllViews();

        for (int i = 0; i < 5; i++) {
            Button b = new Button(this);
            b.setText("Button "+i);
            b.setId(i);
            child=(LinearLayout)findViewById(R.id.lin);
            child.addView(b);

        }
        sc.addView(child);
        main.addView(sc);
        setContentView(main);

    }

}

这是我的代码.....但是应用程序没有运行.....

【问题讨论】:

    标签: java android xml


    【解决方案1】:

    第一步:在ScrollView中添加任意布局容器(如线性布局)

    <ScrollView android:id="@+id/ScrollView01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
       <LinearLayout android:id="@+id/resultContentHolder"
      android:layout_width="fill_parent"
      android:orientation="vertical"
      android:gravity="center_horizontal|top" 
      android:layout_height="fill_parent" />
    </ScrollView> 
    

    第 2 步:在 Activity 类的布局容器中添加任何自定义视图,例如

    LinearLayout gapH = new LinearLayout(this);
                LayoutParams gapParamsH = new LayoutParams(LayoutParams.FILL_PARENT,10);
                gapH.setLayoutParams(gapParamsH);
                resultContentHolder.addView(gapH);   
    

    【讨论】:

      【解决方案2】:

      首先创建一个子布局,您可以在其中创建动态视图。您可以参考link 添加动态视图。

      【讨论】:

        【解决方案3】:

        您确定要为此使用ScrollView 吗?听起来ListView 可能会更好地为您服务。

        如果您需要ListViewthis tutorial on vogella.com 是我所知道的关于如何使用它们的最佳教程之一。

        【讨论】:

          【解决方案4】:
          // Find the ScrollView 
          ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView1);
          
          // Create a LinearLayout element
          LinearLayout linearLayout = new LinearLayout(this);
          linearLayout.setOrientation(LinearLayout.VERTICAL);
          
          // Add Buttons
          Button button = new Button(this);
          button.setText("Some text");
          linearLayout.addView(button);
          
          // Add the LinearLayout element to the ScrollView
          scrollView.addView(linearLayout);
          

          See this link

          【讨论】:

          • 请查看更新后的问题,我无法理解有什么问题
          猜你喜欢
          • 1970-01-01
          • 2016-11-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-08-07
          • 2012-10-21
          相关资源
          最近更新 更多