【发布时间】:2016-02-05 11:35:14
【问题描述】:
我需要动态创建下一个 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 包裹,部分被editText 覆盖。
为什么会这样?
这就是它的样子:
【问题讨论】:
-
在这里发布屏幕截图
-
尝试 layoutParams:LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 3f);或类似的每个视图,也许这会更好
-
@Opiatefuchs,不,这没有帮助(
-
好的,要弄清楚到底是什么不工作,请在可能的情况下发布您的结果的屏幕截图(也许应该是这样)
-
@SuhasB,请查看更新
标签: android text textview word-wrap android-wrap-content