ActionBar - Custom view

本文介绍了如何在Android中激活并实现自定义视图功能,通过设置ActionBar的显示选项,展示自定义布局。示例代码展示了如何切换不同视图,并提供了two_button_hor.xml布局文件的内容,包含一个可切换的Dial和Contacts按钮,以及一个隐藏的编辑电话号码的输入框。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 需要激活自定义视图功能
actionBar.setDisplayShowCustomEnabled(true);
/src/com/wind/actionbar/HelloActionBarActivity.java
package com.wind.actionbar;

import android.app.ActionBar;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

public class HelloActionBarActivity extends Activity{
    private static final String TAG = "HelloActionBarActivity";
	private ActionBar actionBar;
	private Button mButton;
	private LinearLayout mL1;
	private LinearLayout mL2;
	protected boolean mFlagDial = false;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.v(TAG, "onCreate");
        setContentView(R.layout.main);
        actionBar = getActionBar();
        //custom title
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,
                ActionBar.DISPLAY_SHOW_CUSTOM | ActionBar.DISPLAY_SHOW_HOME |
                ActionBar.DISPLAY_SHOW_TITLE);
        //上句相当于:
        /*actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        */
        
        actionBar.setCustomView(R.layout.two_button_hor);
        mButton = (Button) this.findViewById(R.id.button);
        mL1 = (LinearLayout) this.findViewById(R.id.action_layout);
        mL2 = (LinearLayout) this.findViewById(R.id.action_layout_edit);
        
		mButton.setOnClickListener(new OnClickListener(){
			
			@Override
			public void onClick(View v) {
				if(!mFlagDial){
					mL1.setVisibility(View.GONE);
					mL2.setVisibility(View.VISIBLE);
					mFlagDial = true;
				}else{
					mL1.setVisibility(View.VISIBLE);
					mL2.setVisibility(View.GONE);
					mFlagDial = false;
				}
				
				
			}
		});
    }
}
2. 自定义视图
res/layout/two_button_hor.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
	<LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:id="@+id/action_layout"
	    android:background="@color/green"
	    android:orientation="horizontal" >
	    <TextView 
	        android:id="@+id/button_dial"
	        android:text="Dial"
	        android:textSize="30sp"
	        android:gravity="center"
	        android:layout_weight="1"
	        android:layout_width="match_parent"
	        android:layout_height="match_parent" />
	    
		<TextView 
	        android:id="@+id/button_contacts"
	        android:text="Contacts"
	        android:textSize="30sp"
	        android:gravity="center"
	        android:layout_weight="1"
	        android:layout_width="match_parent"
	        android:layout_height="match_parent" />
	</LinearLayout>
	<LinearLayout 
	    android:layout_width="match_parent"
	    android:layout_height="match_parent"
	    android:id="@+id/action_layout_edit"
	    android:visibility="gone"
	    android:background="@color/blue"
	    android:orientation="horizontal" >
	    <EditText 
	        android:id="@+id/edit_number"
	        android:text="13764749630"
	        android:layout_weight="1"
	        android:layout_width="match_parent"
	        android:layout_height="match_parent" />
	    
		<Button 
	        android:id="@+id/button_del"
	        android:text="Del"
	        android:layout_weight="5"
	        android:layout_width="match_parent"
	        android:layout_height="match_parent" />
	</LinearLayout>
</FrameLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值