布局
程序员文章站
2022-04-29 19:53:42
...
package com.doit.huabeiwusheng;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
/**
* Created by wk on 2016/7/7.
* Base Activity.
*/
public abstract class BaseAppCompatActivity extends AppCompatActivity {
private static final String TAG = BaseAppCompatActivity.class.getSimpleName();
private TextView mToolbarTitle;
private TextView mToolbarSubTitle;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getLayoutId());
mToolbar = (Toolbar) findViewById(R.id.toolbar);
/*
toolbar.setLogo(R.mipmap.ic_launcher);
toolbar.setTitle("Title");
toolbar.setSubtitle("Sub Title");
*/
mToolbarTitle = (TextView) findViewById(R.id.toolbar_title);
mToolbarSubTitle = (TextView) findViewById(R.id.toolbar_subtitle);
if (mToolbar != null) {
//将Toolbar显示到界面
setSupportActionBar(mToolbar);
}
if (mToolbarTitle != null) {
//getTitle()的值是activity的android:lable属性值
mToolbarTitle.setText(getTitle());
//设置默认的标题不显示
getSupportActionBar().setDisplayShowTitleEnabled(false);
}
}
@Override
protected void onStart() {
super.onStart();
/**
* 判断是否有Toolbar,并默认显示返回按钮
*/
if(null != getToolbar() && isShowBacking()){
showBack();
}
}
/**
* 获取头部标题的TextView
* @return
*/
public TextView getToolbarTitle(){
return mToolbarTitle;
}
/**
* 获取头部标题的TextView
* @return
*/
public TextView getSubTitle(){
return mToolbarSubTitle;
}
/**
* 设置头部标题
* @param title
*/
public void setToolBarTitle(CharSequence title) {
if(mToolbarTitle != null){
mToolbarTitle.setText(title);
}else{
getToolbar().setTitle(title);
setSupportActionBar(getToolbar());
}
}
/**
* this Activity of tool bar.
* 获取头部.
* @return support.v7.widget.Toolbar.
*/
public Toolbar getToolbar() {
return (Toolbar) findViewById(R.id.toolbar);
}
/**
* 版本号小于21的后退按钮图片
*/
private void showBack(){
//setNavigationIcon必须在setSupportActionBar(toolbar);方法后面加入
getToolbar().setNavigationIcon(R.drawable.ic_find);
getToolbar().setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
}
/**
* 是否显示后退按钮,默认显示,可在子类重写该方法.
* @return
*/
protected boolean isShowBacking(){
return true;
}
/**
* this activity layout res
* 设置layout布局,在子类重写该方法.
* @return res layout xml id
*/
protected abstract int getLayoutId();
@Override
protected void onDestroy() {
super.onDestroy();
Log.v(TAG, "onDestroy...");
}
}
大师傅但是
package com.doit.huabeiwusheng;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;
public class MainActivity extends BaseAppCompatActivity{
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getToolbarTitle().setText("放心住");
/* getSubTitle().setText("更多");*/
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
protected boolean isShowBacking() {
return false;
}
@Override
protected int getLayoutId() {
return R.layout.activity_main;
}
/*public void login(View view) {
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.navigation, menu);
return false;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case R.id.navigation_home:
Toast.makeText(MainActivity.this, ""+"关于", Toast.LENGTH_SHORT).show();
break;
case R.id.navigation_notifications:
Toast.makeText(MainActivity.this, ""+"设置", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
}
大师傅但是
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="@layout/toolbar"/>
<!-- <TextView
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginStart="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />-->
<TextView
android:layout_marginTop="30px"
android:id="@+id/message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="人口的JFK的咖啡店"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimary"
android:minHeight="?attr/actionBarSize">
<!--自定义toolbar的title 和subtitle -->
<TextView
android:id="@+id/toolbar_subtitle"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#fffFff"
android:text="subtitle"
android:paddingRight="10dp"
android:layout_gravity="right" />
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:lines="1"
android:ellipsize="end"
android:text="title"
android:scrollHorizontally="true"
android:textColor="#ffffff"
android:layout_gravity="center" />
</android.support.v7.widget.Toolbar>
的城市
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_zhuye"
android:title="首页" />
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_find"
android:title="发现" />
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_my"
android:tint="#737373"
android:title="我的" />
</menu>
sacs
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
</resources>
上一篇: Java中final关键字详解
下一篇: PHP开发搜索引擎技术教程