直播系統程式碼,自定義平臺私聊對話方塊

zhibo系統開發發表於2021-12-29

直播系統程式碼,自定義平臺私聊對話方塊實現的相關程式碼

1.定義dialog.xml (res/layout/dialog.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
    <RelativeLayout
        android:layout_width="250sp"
        android:layout_height="270sp"
        android:layout_centerInParent="true">
 
        <TextView
            android:id="@+id/dialog_title"
            android:layout_width="wrap_content"
            android:layout_height="35sp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="15sp"
            android:textColor="#333333"
            android:textSize="17sp" />
 
        <TextView
            android:id="@+id/dialog_message"
            android:layout_width="wrap_content"
            android:layout_height="160sp"
            android:layout_centerInParent="true"
            android:layout_marginTop="65sp"
            android:textColor="#333333"
            android:textSize="17sp" />
 
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="30sp"
            android:layout_marginBottom="20sp"
            android:orientation="horizontal">
 
            <LinearLayout
                android:id="@+id/dialog_confirm"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/confirm_button_style"
                    android:gravity="center"
                    android:text="確定"
                    android:textColor="@color/white"
                    android:textSize="18sp" />
            </LinearLayout>
 
            <LinearLayout
                android:id="@+id/dialog_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="22sp">
 
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/cancel_button_style"
                    android:gravity="center"
                    android:text="取消"
                    android:textColor="@color/teal_200"
                    android:textSize="18sp" />
            </LinearLayout>
        </LinearLayout>
 
    </RelativeLayout>
</RelativeLayout>


2. 設定確定、取消按鈕的background

上文的dialog.xml中,確定和取消按鈕都是TextView,所以需要自定義按鈕的背景

confirm_button_style.xml  (所有的color需要自定義)
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="
    android:shape="rectangle">
    <corners android:radius="1000sp"/>
    <solid android:color="@color/teal_200"/>
    <stroke
        android:width="0.5sp"
        android:color="@color/colorAccent"/>
    <size android:width="105sp" android:height="40sp"/>
</shape>
cancel_button_style.xml 
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="
    android:shape="rectangle">
    <corners android:radius="1000sp"/>
    <solid android:color="@color/white"/>
    <stroke
        android:width="0.5sp"
        android:color="@color/teal_200"/>
    <size android:width="105sp" android:height="40sp"/>
</shape>


3. 自定義dialog的使用

final AlertDialog dialog = new AlertDialog.Builder(xxxClass.this).create();
dialog.setCancelable(false); //點選對話方塊以外的位置,不消失
dialog.show();
 
Window window = dialog.getWindow();
window.setContentView(R.layout.dialog);
//標題
TextView title = window.findViewById(R.id.dialog_title);
title.setText("dialog_title");
 
//內容
TextView message = window.findViewById(R.id.dialog_message);
message.setText("dialog_message ");
 
//確定按鈕
LinearLayout confirm = window.findViewById(R.id.dialog_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //xxx
    }
});
 
//取消按鈕
LinearLayout cancel = window.findViewById(R.id.dialog_cancel);
cancel.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        //xxx
    }
});


以上就是直播系統程式碼,自定義平臺私聊對話方塊實現的相關程式碼, 更多內容歡迎關注之後的文章


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69978258/viewspace-2849908/,如需轉載,請註明出處,否則將追究法律責任。

相關文章