TextView 不换行文本
TextView doesn't wrap text
我需要动态创建下一个xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="a lot of text will be here"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
</LinearLayout>
这是我的代码:
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(layoutLP);
TextView title = new TextView(getActivity());
title.setText("a lot of text will be here");
title.setLayoutParams(textLP);
EditText editText = new EditText(getActivity());
editText.setLayoutParams(ediLP);
editText.setSingleLine();
ll.addView(title);
ll.addView(editText);
layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutLP.gravity = Gravity.CENTER;
textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
但是,在设备上我遇到了问题:textView
中的 text
没有被包装,部分被 editText
覆盖。
为什么会这样?
这是它的样子:
试试你的 phone 或 phone 与你在 AVD 上的 dpi 分辨率属性相似,看看是否是同样的问题。可能是你的 phone 的分辨率有问题。
你的Activity应该是
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutLP.gravity = Gravity.CENTER;
layoutLP.weight = 2;
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
LinearLayout.LayoutParams editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
// Add textview 1
TextView textView1 = new TextView(this);
textView1.setLayoutParams(textLP);
textView1.setText("programmatically created TextView1");
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(textView1);
// Add textview 1
EditText editText = new EditText(this);
editText.setLayoutParams(editLP);
editText.setHint("programmatically created TextView1");
// editText.setBackgroundColor(Color.GRAY); // hex color 0xAARRGGBB
editText.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(editText);
linearLayout.addView(linearLayout1);
您的 xml 文件应该是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/ll"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
我需要动态创建下一个xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="a lot of text will be here"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
</LinearLayout>
这是我的代码:
LinearLayout ll = new LinearLayout(getActivity());
ll.setLayoutParams(layoutLP);
TextView title = new TextView(getActivity());
title.setText("a lot of text will be here");
title.setLayoutParams(textLP);
EditText editText = new EditText(getActivity());
editText.setLayoutParams(ediLP);
editText.setSingleLine();
ll.addView(title);
ll.addView(editText);
layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutLP.gravity = Gravity.CENTER;
textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
但是,在设备上我遇到了问题:textView
中的 text
没有被包装,部分被 editText
覆盖。
为什么会这样?
这是它的样子:
试试你的 phone 或 phone 与你在 AVD 上的 dpi 分辨率属性相似,看看是否是同样的问题。可能是你的 phone 的分辨率有问题。
你的Activity应该是
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
LinearLayout.LayoutParams layoutLP = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
layoutLP.gravity = Gravity.CENTER;
layoutLP.weight = 2;
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setLayoutParams(layoutLP);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams textLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
textLP.weight = 2;
textLP.gravity = Gravity.CENTER;
LinearLayout.LayoutParams editLP = new LinearLayout.LayoutParams(
0, LinearLayout.LayoutParams.WRAP_CONTENT);
editLP.weight = 3;
editLP.gravity = Gravity.CENTER;
// Add textview 1
TextView textView1 = new TextView(this);
textView1.setLayoutParams(textLP);
textView1.setText("programmatically created TextView1");
// textView1.setBackgroundColor(0xff66ff66); // hex color 0xAARRGGBB
textView1.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(textView1);
// Add textview 1
EditText editText = new EditText(this);
editText.setLayoutParams(editLP);
editText.setHint("programmatically created TextView1");
// editText.setBackgroundColor(Color.GRAY); // hex color 0xAARRGGBB
editText.setPadding(20, 20, 20, 20);// in pixels (left, top, right, bottom)
linearLayout1.addView(editText);
linearLayout.addView(linearLayout1);
您的 xml 文件应该是:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout android:id="@+id/ll"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>