安卓APP_ 控件(7)——Toolbar栏目样式
生活随笔
收集整理的這篇文章主要介紹了
安卓APP_ 控件(7)——Toolbar栏目样式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
摘自:安卓APP_ 控件(7)——Toolbar欄目樣式
作者:丶PURSUING
發布時間: 2021-04-02 15:42:07
網址:https://blog.csdn.net/weixin_44742824/article/details/115395997
Toolbar欄目樣式
- 什么是Toolbar?
- 設置細節見具體代碼
- ToolBar當然也可以在.java中進行設置
- 控件的嵌套運用:TextView放在Toolbar中
- ToolBar的導包問題
什么是Toolbar?
改為NoActionBar,創建屬于自己的欄目樣式
如下圖,自定義了Toolbar樣式和其中的navigationIcon返回設置
設置細節見具體代碼
activity_main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><!-- ?attr/actionBarSize 設置高度為系統的actionBar高度logo 很少設置navigationIcon 這個圖標一般多設置為返回箭頭subtitle 子標題一般少設置,出現在主標題的下方subtitleTextColor 子標題文本顏色title 當前頁的主標題titleMarginStart 主標題距離左邊的間距titleTextColor 主標題文本顏色設置前綴為app表示用的是androidx里面的屬性而不是安卓自帶的 --><androidx.appcompat.widget.Toolbarandroid:id="@+id/tool_return"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:background="@color/teal_200"app:logo="@drawable/ic_baseline_loyalty_24"app:navigationIcon="@drawable/ic_baseline_keyboard_return_24"app:subtitle=" 監控中"app:subtitleTextColor="#ff0000"app:title="智能家居"app:titleMarginStart="90dp"app:titleTextColor="@color/purple_700" /></LinearLayout>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
MainActivity.java
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Toolbar toolbar = findViewById(R.id.tool_return);//這個返回的圖標等待被點擊,點擊后執行的操作。toolbar.setNavigationOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.e("zhua", "onClick: toolbar被點擊了");}});} }- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
ToolBar當然也可以在.java中進行設置
例如:
MainActivity.java
Toolbar toolbar2 = findViewById(R.id.tool_return_2); toolbar2.setNavigationIcon(R.drawable.ic_baseline_keyboard_return_24); setTitle("標題"); toolbar2.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Log.e("zhua", "onClick: toolbar2被點擊了");} });- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
控件的嵌套運用:TextView放在Toolbar中
如何把主標題放在正中間呢?老知識新用法罷了,控件的組合:TextView放在Toolbar中
<androidx.appcompat.widget.Toolbarandroid:id="@+id/tool_return_2"android:layout_width="match_parent"android:layout_height="?attr/actionBarSize"android:layout_marginTop="20dp"android:background="#ffff00"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:gravity="center"android:text="TextView標題"android:textSize="22dp"android:textColor="#ff0000"android:textStyle="bold" /></androidx.appcompat.widget.Toolbar>- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
效果如下圖:
ToolBar的導包問題
可能會遇到的ToolBar的導包問題:
要把下面這個修改
導入正確的包
總結
以上是生活随笔為你收集整理的安卓APP_ 控件(7)——Toolbar栏目样式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端小结(5)---- iframe
- 下一篇: 「数据新星」Databricks 崛起启