Java 如何在android中包装动态创建的文本视图?
我正在线性布局层内创建各种文本视图。我希望当文本视图伸出屏幕时,文本视图会自动跳转到下一行,而不是跳出屏幕。我是android的初学者,不知道如何实现它 这是我的密码:Java 如何在android中包装动态创建的文本视图?,java,android,android-layout,Java,Android,Android Layout,我正在线性布局层内创建各种文本视图。我希望当文本视图伸出屏幕时,文本视图会自动跳转到下一行,而不是跳出屏幕。我是android的初学者,不知道如何实现它 这是我的密码: for (int i = 0; i < string.size(); i++) { final TextView txt = new TextView(con); txt.setText(string.get(i)); lay.addView(txt);
for (int i = 0; i < string.size(); i++) {
final TextView txt = new TextView(con);
txt.setText(string.get(i));
lay.addView(txt);
}
试试这个
TextView txt = new TextView(context);
txt.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
这个方法应该可以帮助你。我很久以前就已经使用过这个方法了,所以只要稍作修改,您就可以使用它了。第一个参数是LinearLayout,它将是作为视图主容器的基本垂直线性布局。第二个参数是必须添加的实际视图。在我的例子中,第三个参数是一个像占位符一样使用的图像。您也可以这样添加
for (int i = 0; i < string.size(); i++) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(
R.layout.Layout_Design, null);
TextView txt = (TextView) view.findViewById(R.id.some_text);
txt.setText("for runtime change");
layout.addView(view);
// layout is linear layout in my case.
}
布局设计:
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams.WRAP_内容,LayoutParams.WRAP_内容;tv.setLayoutParamslp;抱歉@Jitendraramoliya它在我的情况下不起作用。您的文本在textView中设置什么?它是一个类似字符串字符串[]={a,b………}的数组字符串中的值@我想知道我的答案。
for (int i = 0; i < string.size(); i++) {
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = (View) inflater.inflate(
R.layout.Layout_Design, null);
TextView txt = (TextView) view.findViewById(R.id.some_text);
txt.setText("for runtime change");
layout.addView(view);
// layout is linear layout in my case.
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="80dp"
android:layout_height="80dp" >
<RelativeLayout
android:layout_width="80dp"
android:layout_height="80dp"
android:paddingBottom="5dp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="5dp" >
<TextView
android:id="@+id/some_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SOme Text" />
</RelativeLayout>
</FrameLayout>