国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android 自定義狀態欄實例代碼

2019-10-23 19:47:16
字體:
來源:轉載
供稿:網友

一、目標:Android5.0以上

二、步驟

1、在res-values-colors.xml下新建一個RGB顏色

  <?xml version="1.0" encoding="utf-8"?>   <resources>     <color name="colorPrimary">#3F51B5</color>     <color name="colorPrimaryDark">#303F9F</color>     <color name="colorAccent">#FF4081</color>     <color name="theRed">#ff6a69</color>   </resources> 

2、新建一個布局,名為actionbarlayout.xml,在后邊重寫布局時用于添加

<?xml version="1.0" encoding="utf-8"?>   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:orientation="vertical"     android:layout_width="match_parent"     android:layout_height="wrap_content">     <TextView       android:id="@+id/actionBarId"       android:layout_width="match_parent"       android:layout_height="wrap_content"       />   </LinearLayout> 

3、重寫LineaLayout布局,放置步驟二新建的布局在頂部,用作背景顏色的容器

  public class ActionBarLayOut extends LinearLayout {     public ActionBarLayOut(Context context, AttributeSet attrs){       super(context,attrs);       LayoutInflater.from(context).inflate(R.layout.actionbarlayout,this);     }   }

3、在主布局里調用這個重寫后的線性布局

<?xml version="1.0" encoding="utf-8"?>   <com.example.test.ActionBarLayOut xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:id="@+id/activity_main"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     tools:context="com.example.test.MainActivity">     <TextView       android:layout_width="match_parent"       android:layout_height="wrap_content"       android:background="@color/theRed"       android:text="Hello World!" />   </com.example.test.ActionBarLayOut> 

 4、在main活動中進行相應設置

public class MainActivity extends AppCompatActivity {     @Override     protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       TextView textView = (TextView)findViewById(R.id.actionBarId);       int color = getResources().getColor(R.color.theRed);       setActionBarColor(textView,color);     }     protected void setActionBarColor(TextView textView, int ActionBarColor){       //----------------------------------隱藏標題欄----------------------------------------------       if (getSupportActionBar()!=null) {         getSupportActionBar().hide();       }       //------------------------------------------------------------------------------------------       //----------------------------------將狀態欄設置為透明--------------------------------------       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {         Window window = getWindow();         window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS             | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);         window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN             | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION             | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);         window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);         window.setStatusBarColor(Color.TRANSPARENT);         window.setNavigationBarColor(Color.TRANSPARENT);       }       //------------------------------------------------------------------------------------------       /**        * 首先獲取狀態欄的高度statusBarHeight1,然后在狀態欄的位置放一個空的TextView,        * 高度設置為statusBarHeight1,然后將TextView的背景顏色進行設置,這樣就可以變相        * 的給狀態欄設置顏色        */       int statusBarHeight1 = -1;       //獲取status_bar_height資源的ID       int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");       if (resourceId > 0) {         //根據資源ID獲取響應的尺寸值         statusBarHeight1 = getResources().getDimensionPixelSize(resourceId);       }       textView.setHeight(statusBarHeight1);       textView.setBackgroundColor(ActionBarColor);     }   } 

以上所述是小編給大家介紹的Android 自定義狀態欄實例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對VEVB武林網網站的支持!


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长海县| 望城县| 本溪市| 清流县| 广德县| 长沙市| 普陀区| 内江市| 年辖:市辖区| 石屏县| 正安县| 汾西县| 临清市| 定南县| 松溪县| 察雅县| 安远县| 龙里县| 常熟市| 莫力| 开封县| 东方市| 扎兰屯市| 普格县| 准格尔旗| 田林县| 安岳县| 山丹县| 裕民县| 东城区| 彭阳县| 财经| 甘孜| 莲花县| 时尚| 嘉定区| 莱西市| 虎林市| 镇平县| 临沭县| 淮安市|