android - 以编程方式将 textView 添加到 LinearLayout。已添加项目,但似乎是空的

标签 android android-layout

我需要带有 image 和 textViews 的 scrollableView。

我有这样的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffffff">

    <EditText
        android:id="@+id/comment_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_toLeftOf="@+id/addCommentButton"
        android:inputType="text"
        android:textColor="#ff000000" />

    <Button
        android:id="@+id/addCommentButton"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/comment_text"
        android:text="Send" />

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/comment_text"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="top">

            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="description"
                android:maxHeight="300dp" />

            <LinearLayout
                android:id="@+id/comments"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:orientation="vertical">
            </LinearLayout>

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

然后我尝试添加新项目。

@ViewById
protected LinearLayout comments;

TextView textView = new TextView(this);
textView.setTextColor(Color.BLACK);
textView.setText(comment.getMessage());
comments.addView(textView, 0);

我明白了,但是 comment.getMessage() 不是空的。 我做错了什么?

请看截图

http://i.stack.imgur.com/8ChWy.png http://i.stack.imgur.com/ZGxSQ.png

最佳答案

尝试这种方式,希望这能帮助您解决问题。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffffff"
    android:orientation="vertical"
    android:padding="5dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="top"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/image"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:contentDescription="description"
                android:adjustViewBounds="true"
                android:maxHeight="300dp" />

            <LinearLayout
                android:id="@+id/comments"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#ffffffff"
                android:orientation="vertical">
            </LinearLayout>

        </LinearLayout>
    </ScrollView>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp">
        <EditText
            android:id="@+id/comment_text"
            android:layout_width="0dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:textColor="#ff000000" />

        <Button
            android:id="@+id/addCommentButton"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:text="Send" />
    </LinearLayout>

</LinearLayout>

关于android - 以编程方式将 textView 添加到 LinearLayout。已添加项目,但似乎是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25076842/

相关文章:

android - 剪辑按钮不接收触摸输入

java - 将数据从一个 Activity 发送到另一类(不是 Activity )

android - 了解与 Activity 有关的 FragmentManager 和 FragmentTransaction 生命周期

android - 在 Android 中创建和使用 9-patch 图像

android - 无效的LOC header

java - 使用包装内容时获取宽度和高度

android - 在 layout.main 上崩溃

android - 搜索框 - 如何在代码中打开

java - 使用媒体播放器播放 .M3U 链接

android - 插孔内存不足