【问题标题】:TextView doesn't wrap textTextView 不换行
【发布时间】: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


【解决方案1】:

你的活动应该是

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>

【讨论】:

  • 这看起来像是魔法之王......你和我的代码没有太大区别......但经过一些重构后它现在可以工作......谢谢!
【解决方案2】:

在 AVD 上尝试您的手机或与您的 dpi 分辨率属性相似的手机,看看是否是同样的问题。可能是您手机上的分辨率有问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-28
    • 1970-01-01
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多