TextView不会包装文本

问题描述 投票:0回答:2

我需要动态创建下一个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;

然而,在设备上我遇到问题:text内部的textView没有包裹,部分被editText覆盖。

为什么会这样?

以下是它的样子:

android text textview textwrapping android-wrap-content
2个回答
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>

Here Screen Shot


0
投票

尝试使用您的手机或具有与AVD相似的dpi分辨率属性的手机,看看它是否是同一个问题。也许手机上的分辨率是问题所在。

© www.soinside.com 2019 - 2024. All rights reserved.