Android也是一点一滴自学来的,自定义标题栏费了一番功夫,还是Android功底太弱,记录一下。
import com.hf.mobsign.util.Constant;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
@SuppressWarnings("deprecation")
public class PatientTabActivity extends TabActivity {
private Button but;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.maind);
but=(Button)findViewById(R.id.tabback);
Toolbar toolBar= (Toolbar) findViewById(R.id.toolbar);
Bundle bundle = getIntent().getExtras();
String userName = bundle.getString("userName");
Constant.userName=userName;
toolBar.setTitle("欢迎"+userName);
TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
tab.setup();
tab.addTab(tab
.newTabSpec("tab1")
.setIndicator("A")
.setContent(new Intent().setClass(this, WaitSignActivity.class)));
tab.addTab(tab
.newTabSpec("tab2")
.setIndicator("B")
.setContent(new Intent().setClass(this, SignedActivity.class)));
}
public void tabBck(View view){
PatientTabActivity.this.finish();
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#ADADAD">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp">
</android.support.v7.widget.Toolbar>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" >
<Button
android:id="@+id/tabback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="3.0dip"
android:text="退出"
android:onClick="tabBck"/>
</LinearLayout>
</LinearLayout>
<TabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!-- TabWidget组件id值不可变 -->
<TabWidget
android:id="@android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<!-- FrameLayout布局,id值不可变 -->
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@android:id/tabs" >
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
最终效果如下