直播系統搭建,Android使用RadioGroup+RadioButton實現導航欄

zhibo系統開發發表於2023-03-27

直播系統搭建,Android使用RadioGroup+RadioButton實現導航欄

主頁面程式碼

package com.example.taobaounion.ui.activity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import com.example.taobaounion.R;
import com.example.taobaounion.utils.LogUtils;
import butterknife.BindView;
import butterknife.ButterKnife;
public class TextActivity extends Activity {
    @BindView(R.id.test_navigation_bar)
    public RadioGroup navigation_bar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text);
        ButterKnife.bind(this);
        initListener();
    }
    private void initListener() {
        navigation_bar.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            //對控制元件ID進行判斷,執行相應的事件
                switch (checkedId){
                    case R.id.test_home:
                        LogUtils.d(TextActivity.class,"首頁");
                        break;
                    case R.id.test_red_packet:
                        LogUtils.d(TextActivity.class,"特惠");
                        break;
                    case R.id.test_select:
                        LogUtils.d(TextActivity.class,"精選");
                        break;
                    case R.id.test_search:
                        LogUtils.d(TextActivity.class,"搜尋");
                        break;
                }
            }
        });
    }
}

主頁面佈局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <RadioGroup
        android:id="@+id/test_navigation_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">
        <RadioButton
            android:id="@+id/test_home"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_home"
            android:gravity="center"
            android:checked="true"
            android:text="首頁"
            android:textColor="@drawable/naviagation_bar_color" />
        <RadioButton
            android:id="@+id/test_select"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_select"
            android:gravity="center"
            android:text="精選"
            android:textColor="@drawable/naviagation_bar_color" />
        <RadioButton
            android:id="@+id/test_red_packet"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_redpacket"
            android:gravity="center"
            android:text="特惠"
            android:textColor="@drawable/naviagation_bar_color" />
        <RadioButton
            android:id="@+id/test_search"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:drawableTop="@drawable/selector_navigation_search"
            android:gravity="center"
            android:text="搜尋"
            android:textColor="@drawable/naviagation_bar_color" />
    </RadioGroup>
    <!--在導航欄上加一條線-->
    <View
        android:layout_width="wrap_content"
        android:layout_height="1px"
        android:layout_marginBottom="10dp"
        android:background="@color/gray"
        android:layout_above="@id/test_navigation_bar"
        />
</RelativeLayout>


分別對四個icon進行狀態設定,選中的時候為黃色,按壓的時候為灰色,預設為黑色

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=">
    <!--state_pressed:表示當按壓的時候的變化 state_checked:表示當選中的時候-->
<item android:drawable="@mipmap/home_checked" android:state_checked="true"/>
<item android:drawable="@mipmap/home_press" android:state_pressed="true"/>
<item android:drawable="@mipmap/home_normal"/>
</selector>


 以上就是 直播系統搭建,Android使用RadioGroup+RadioButton實現導航欄,更多內容歡迎關注之後的文章


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

相關文章