android_標題欄的實現方法
阿新 • • 發佈:2018-11-05
標題欄有很多實現方法今天介紹一種
效果圖
主要是利用visibility的顯示與隱藏
titlebar:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:paddingLeft="10dp" android:background="@color/blue" android:paddingRight="10dp" android:layout_height="50dp"> <ImageView android:id="@+id/iv_home" android:visibility="gone" android:layout_centerVertical="true" android:src="@drawable/home" android:layout_width="30dp" android:layout_height="30dp" /> <ImageView android:id="@+id/iv_back" android:visibility="gone" android:layout_centerVertical="true" android:src="@drawable/back" android:layout_width="30dp" android:layout_height="30dp" /> <TextView android:id="@+id/tv_title" android:textSize="20sp" android:textColor="#fff" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
activity_main.xml:
這個頁面include一下上面寫好的titlebar就行
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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" tools:context="com.example.iamchan.titlebartest.MainActivity"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <include layout="@layout/titlebarlayout"></include><ImageView android:onClick="doClick" android:id="@+id/iv_img" android:background="@mipmap/ic_launcher" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </RelativeLayout>
MainActiivty:
public class MainActivity extends AppCompatActivity { private ImageView home; private TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); home= (ImageView) findViewById(R.id.iv_home); title= (TextView) findViewById(R.id.tv_title); home.setVisibility(View.VISIBLE); title.setText("mainActivity標題欄"); } public void doClick(View view){ if(view.getId()==R.id.iv_img){ startActivity(new Intent(MainActivity.this,SecondActivity.class)); } } }
activity_second:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_second"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.iamchan.titlebartest.SecondActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/titlebarlayout"></include>
</LinearLayout>
</RelativeLayout>
SecondActivity:
public class SecondActivity extends AppCompatActivity { private ImageView back; private TextView title; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); back= (ImageView) findViewById(R.id.iv_back); title= (TextView) findViewById(R.id.tv_title); back.setVisibility(View.VISIBLE); title.setText("secondActivity標題欄"); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); } }