【问题标题】:Android - add lines dynamically to layoutAndroid - 动态添加线条到布局
【发布时间】:2018-01-19 14:47:11
【问题描述】:

我是 Android 新手。

请相应地更改代码。

想要动态显示短信,它们都是异步调用,因为我从异步调用得到响应,我需要在屏幕上显示短信。它们的顺序可能不同,如下所示。

不应等到所有异步调用完成并使用 StringBuilder 一起显示。这不是我正在寻找的解决方案。

当我收到回复时,我需要逐行显示消息(如果可能,请使用小勾号)。行数不固定,可以是 20 或 50。

谢谢。

main.xml

    <RelativeLayout
        android:id="@+id/llMessages"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:visibility="visible">

        <TextView
            android:id="@+id/message"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="40dp"
            android:layout_marginTop="65dp"
            android:inputType="textMultiLine"
            android:singleLine="false"
            android:textIsSelectable="false"/>

    </RelativeLayout>

MainActivity.java

TextView msgs = findViewById(R.id.message);
msgs.setText("First\n"); 
msgs.setText("Second\n"); //<-- Text comes 3 seconds later - Async calls
msgs.setText("Third\n"); //<-- Text comes 3 seconds later - Async calls
msgs.setText("Fourth\n"); //<-- Text comes 3 seconds later - Async calls
msgs.setText("Fifth\n"); //<-- Text comes 3 seconds later - Async calls

【问题讨论】:

标签: android android-layout


【解决方案1】:

每当有新文本可用时,使用此方法附加文本append()。把 \n 换行

【讨论】:

    【解决方案2】:

    使用具有垂直方向的 LinearLayout 而不是 xml 中的 TextView。 在 Java 中创建 TextView

    每次从服务器获取字符串时,都会在 java 中创建新的 TextView 并将其添加到 LinearLayout 中。 这是示例代码。

    LinearLayout linearLayout =  (LinearLayout) findViewById(R.id.linear_layout_id);
    
    TextView tv = new TextView(this);
    tv.setText("FirstText");
    tv.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
    
    linearLayout.addView(tv);
    

    【讨论】:

    • 感谢您的信息,这有帮助。不知道为什么人们会降级答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-25
    • 1970-01-01
    相关资源
    最近更新 更多