Android 如何以编程方式在单个滚动条中添加多个文本框
我有下面的代码问题是当我运行这个程序时,所有的文本框都不会显示在屏幕上,所以我想我需要在scrollview中添加所有的文本框,但我不知道如何添加。我也可以使用listview来做这件事,但我必须通过java代码中的编程方式添加scrollview来完成。帮助任何人Android 如何以编程方式在单个滚动条中添加多个文本框,android,android-layout,textbox,scrollbar,scrollview,Android,Android Layout,Textbox,Scrollbar,Scrollview,我有下面的代码问题是当我运行这个程序时,所有的文本框都不会显示在屏幕上,所以我想我需要在scrollview中添加所有的文本框,但我不知道如何添加。我也可以使用listview来做这件事,但我必须通过java代码中的编程方式添加scrollview来完成。帮助任何人 protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(
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());
}
}
}
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
字符串[]textArray={“一”、“二”、“asdasasdf”、“asdf”、“dsdaa”、“fsvs”、“sd”};
int length=textArray.length;
LinearLayout布局=新的LinearLayout(本);
setContentView(布局);
布局。设置方向(线性布局。垂直);
对于(int i=0;i在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>
在此处动态添加TextBox
:
TextView textBox = new TextView(context);
layout.addView(textBox);
//试试这个方法,希望对你有帮助。。。
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
字符串[]textArray={“一”、“二”、“asdasasdf”、“asdf”、“dsdaa”、“fsvs”、“sd”};
int length=textArray.length;
ScrollView ScrollView=新的ScrollView(此);
LinearLayout布局=新的LinearLayout(本);
布局。设置方向(线性布局。垂直);
对于(int i=0;i请输入您的xml代码
TextView textBox = new TextView(context);
layout.addView(textBox);
// 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);
}