簡體   English   中英

在 Java 和 XML 中使用自定義布局

[英]Using custom layouts in Java and XML

我正在 Android Studio 中制作一個聊天應用程序,並且我編寫了幾個 XML 布局文件來顯示消息:

氣泡發送.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/bubble_send_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/bubble_send_user"
        android:layout_alignParentLeft="true"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="300dp"
        android:padding="10dp"
        android:layout_margin="16dp"
        android:textAlignment="center"
        android:textSize="16dp"
        android:text="Me" />

    <TextView
        android:id="@+id/bubble_send_text"
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="300dp"
        android:background="@drawable/bubble_send_bg"
        android:padding="10dp"
        android:layout_margin="16dp"
        android:textAlignment="center"
        android:textSize="24dp"
        android:text="Dummy text send" />

</RelativeLayout>

和 bubble_receive.xml 基本相同,只是對齊方式不同。

我想用 Java 和 append 將 RelativeLayout 中的兩個 TextViews 的文本值更改為聊天活動中垂直對齊的 LinearLayout 作為消息框架。

我已經嘗試研究此事,但我只能找到使用 ListView 和 RecyclerView 而不是 LinearLayout 的解決方案,這不適合我的任務。

我怎樣才能了解它?

以嵌套滾動視圖添加父線性布局,然后每當您收到消息時嘗試使用兩個 textview 設置消息創建子線性布局 textview 然后添加到子線性布局。 滾動視圖-垂直方向的父線性布局-(Child1 Text11 Text12)(Child2 Text21 Text22)......?

您需要做的是創建將繼承RelativeLayout的自定義視圖。 如果你在 Java 中編碼,你應該寫這樣的東西。

public class MyView extends RelativeLayout {

    private TextView sendUserBubble;

    public MyView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyView(Context context) {
        super(context);
        ini();
    }

    private void initView() {
        // Pass your xml layout here that you already created
        inflate(getContext(), R.layout.your_custom_layout, this);
        sendUserBubble = findViewById(R.id.bubble_send_user);
    }
    public void setSendTextUser(String text) {
        sendUserBubble.setText(text)
    }

之后,您在聊天活動 xml 文件中使用它,如下所示:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/chat_activity_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <MyView
        android:id="@+id/my_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

並在您的活動代碼中引用它:

MyView view = findViewById(R.id.my_view)
view.setSendTextUser("My awesome text")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM