TextView无法显示文本(一个TextView将另一个推到屏幕远处)

3
现在,如果我向下滚动到底部,文本视图将显示该语句。看起来 Promise TextView 变得太大了。有没有办法修复它?
下面的所有内容都是 Promise TextView 的一部分。它是一个带有多个换行符的单个字符串。
这是生成 Promise 的代码。 screenshot 另一张截图: screenshot2
prm = doc.getElementsByTagName("promise");
            for (int j = 0; j < prm.getLength(); j++) {
                prom += parser.getValue(e, KEY_PROMISES, j) + "\n";

            }
            prom.substring(0,prom.lastIndexOf('\n'));

布局
    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget32"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <RelativeLayout
        android:id="@+id/top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/face"
            android:layout_width="68dp"
            android:layout_height="68dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true" />

        <TextView
            android:id="@+id/name"
            android:layout_width="243dp"
            android:layout_height="35dp"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@id/face"
            android:text="from" />

        <TextView
            android:id="@+id/office"
            android:layout_width="242dp"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@+id/face"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/name"
            android:layout_toRightOf="@id/face"
            android:text="subject" />
    </RelativeLayout>

    <RatingBar
        android:id="@+id/ratingBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/top"
        android:numStars="6"
        android:singleLine="false" />

    <TextView
        android:id="@+id/promises"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/ratingBar1"
        android:layout_marginTop="5dp"
        android:text="TextView" />

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/promises"
        android:layout_marginRight="22dp"
        android:layout_marginTop="26dp" >

        <TextView
            android:id="@+id/statement"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </ScrollView>

</RelativeLayout>

我的应用程序活动
public class SingleMenuItemActivity  extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.candidate);
        Log.d("here ?", "No ");
        int loader = R.drawable.loader;
        // getting intent data
        Intent in = getIntent();
        Log.d("here ?", "No ");
        // Get XML values from previous intent
        String name = in.getStringExtra("name");
        Log.d("Name ", name);
        Log.d("here ?", "No ");
        DatabaseHandler db = new DatabaseHandler(this);
        Log.d("here ?", "No ");
        Candidate p = db.getCandidate(name);
        Log.d("Did i take candidate? ", "yea ");
        db.close();
        Log.d("Statement", p.get_statment());
        // Displaying all values on the screen
        TextView lblName = (TextView) findViewById(R.id.name);
        TextView lblOffice = (TextView) findViewById(R.id.office);
        TextView lblPromise = (TextView) findViewById(R.id.promises);
        TextView lblStatement = (TextView) findViewById(R.id.statement);
        ImageView lblFace = (ImageView) findViewById(R.id.face);
        RatingBar lblRating = (RatingBar) findViewById(R.id.ratingBar1);
        ImageLoader imgLoader = new ImageLoader(this);
        lblName.setText(p.get_name());
        lblOffice.setText(p.get_office());
        lblPromise.setText(p.get_promises());
        lblStatement.setText(p.get_statment());
        lblRating.setRating(p.get_ranking());
        imgLoader.DisplayImage(p.get_photograph(), loader, lblFace);

    }
}

logcat显示p.get_statement()中有正确的值。

12-16 05:07:16.089: D/Statement(279): I strive for the highest standards of appearance and personal grooming.  If, like me, you think that the University's reputation is being dragged down by lecturers who have little or no regard for fashion and are content to wear ill-fitting or patched clothes then vote for me: the stylish choice.

声望不足以发布图片 %)

它是否显示其他文本,还是空白或缺失? - iagreen
TextView语句完全为空,其他都显示为扩展。 - Yarh
3个回答

3

看起来您的评分条覆盖了语句文本视图:

<RatingBar
    android:layout_above="@+id/statement"
    ... />

也许你想说的是layout_below

另外,你的promises TextView有一个非常大的上边距,这可能会把statement TextView 推到屏幕底部之外:

<TextView
    android:id="@+id/promises"
    android:layout_marginTop="124dp"
    ... />

补充说明
显然,在你的 promises TextView 的末尾还有一些换行符。如果你不能从源代码中删除这些字符,可以使用trim()方法:

prom = prom.trim();

注释的编辑在这里不会自动更新...另外,由于某些原因,图片无法加载,您能否将其发布到imgur.com并发布新链接? - Sam
请明确一点:您所展示的文本是您的promises文本吗?它是否包含许多换行符...? - Sam
它有几个换行符,但是它是一个单独的字符串。 - Yarh
1
我几乎没有更多的想法了... :( 你的ScrollView应该是根布局,widget32 是它的子项。 - Sam
1
你可以随时使用 trim() 来删除字符串前后的任何空格。 - Sam
显示剩余6条评论

2

您的代码似乎是正确的。 如果id为lblStatement的textView没有显示文本,那么可能是由于背景色与文本颜色相近(请尝试更改颜色),或者textView的边距不合适。

如果p.get_statment()返回正确的值,则没有其他可能的原因。


0

好的,我找到了解决方案。而且我不明白为什么文本视图会这么大,但我现在知道如何修复它:) 首先,我们需要知道字符串会产生多少行。

private static int countLines(String str){
           String[] lines = str.split("\r\n|\r|\n");
           return  lines.length;
        }

然后只需限制TextView的大小

lblPromise.setMaxLines(countLines(p.get_promises()));

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接