當前位置:首頁 » 安卓系統 » android自定義actionbar

android自定義actionbar

發布時間: 2022-06-25 13:16:39

① 在Android開發中還有必要去研究和使用ActionBar嗎

首先你要考慮兼容的問題,還要考慮裡面提供的各種生硬煩人的api和亂七八糟的設置,到頭來還是很難自定義化,真是浪費了精力,成效還不大。
現在手上有個項目考慮到要在ActionBar上面加一些東西,我果然的自定義了一個layout,然後再include進去,想怎麼變就怎麼變。但我又在想,既然有actionBar的存在,就有它存在的意義,我是不是還沒有掌握到,所以覺得不好用呢

② 如何調整Android ActionBar向上按鈕與屏幕左側的距離

可行的解決辦法是:
(1) 我想到的最直接的辦法是重新切一張圖,理由如下:
Google官網給出的「左向箭頭」的material icon,就包含上下左右間距:
(2) 看你的代碼,你已經為action bar提供了自定義的布局R.layout.my_booking_actionbar,為什麼不做的徹底一些呢?放棄使用android提供的Home鍵android.R.id.home作為UpIndicator。

而是在這個布局中添加一個ImageView,這樣就可以自定義你需要的間距離。然後實現ImageView.OnClickListener,點擊時返回父activity。
private void initActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
actionBar.setCustomView(R.layout.actionbar_crime_fragment);
View actionView = actionBar.getCustomView();

ImageView upButton = (ImageView) actionView.findViewById(R.id.actionBarUp);
upButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Providing Up Navigation,提供了一種返回父Activity的方法,
// 同時需要在manifest中增加元數據標簽,來定義它的父Activity;
// 詳情請參考以下鏈接,
// http://developer.android.com/training/implementing-navigation/ancestral.html
if (NavUtils.getParentActivityName(CrimePagerActivity.this) != null) {
NavUtils.navigateUpFromSameTask(CrimePagerActivity.this);
}
}
});
}

ActionBar的自定義布局文件actionbar_crime_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/actionBarUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="4dp"
android:background="@drawable/ic_keyboard_arrow_left_white_24dp"
android:contentDescription="@string/app_name"
android:scaleType="fitXY" />

<TextView
android:id="@+id/actionBarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

Done.

③ 如何在fragment中自定義actionbar

一、使用最新的adt插件創建android項目時,如果選擇的MinimumRequiredSDK為8,而TargetSDK大於它的話,系統會自動在項目中導入Supportv4包;在創建項目向導最後一步可以選擇NavigationType,如果選擇了NavigationDrawer,adt工具會在創建項目時自動生成DrawerLayout相關示例代碼。但由於DrawerLayout是在高版本的API中出現的,因此adt工具會幫助導入Supportv7appcompat包,這樣DrawerLayout就可以兼容到Android2.2了。二、首先打開res目錄下的style文件,自定義一個主題和actionbar的style,然後在自定義主題中引用自定義的actionbar的style:這里要注意的是無論是在自定義主題還是自定義style時,要根據情況加上parent屬性,如果沒有加上相應的parent屬性的話就不能使用父style中沒有被覆蓋的樣式。完成自定義主題和style後要記得在manifest文件中應用:這里可以讓整個應用都使用自定義的主題,也可以指定單個activity使用,使用android:theme屬性來指定。三、接下來要給app添加DrawerLayout了,修改MainActivity的布局文件,添加一個DrawerLayout,其中包含一個Drawer和內容布局的Container:注意,下面那個fragment就是app的Drawer,其中的屬性android:layout_gravity在這里表示Drawer從哪一側劃出,start代表左側,end代表右側;還可以定義兩個fragment,然後一個左側劃出一個右側劃出。四、創建完DrawerLayout布局後,來為Drawer定義一個fragment,用ListView加上headerView來實現,創建一個NavigationDrawerFragment繼承自Fragment,這個fragment的布局包含一個ListView:使用一個ArrayList來存放ListView的數據,定義一個DrawerListItem對象來存放每個Item的title和icon的資源ID:

④ 安卓開發android studio中怎樣自定義actionbar的布局

1theme是用於application或activity的。首先打開AndroidManifest文件查看,一般application節點都有默認主題,

2接下來打開上圖中theme所在的文件。res-->values-->styles。

3打開後。可以看到,name屬性正是步驟一中theme的值。在可以看到parent屬性的值,parent是用於繼承內置樣式的。我們接下來要在該樣式的基礎上修改。

4修改action bar的背景。可以從圖中看到,都是一個引用另一個。圖中黃色高亮的部分,是為了兼容性,可以看到其實值是相同的。在這個例子中,因為theme的parent是Theme.AppCompat.Light.DarkActionBar真正起作用的是不帶『android:』前綴的語句,是為了支持低版本的兼容包。而帶前綴的語句是API 11以上支持的。

5修改布局背景。這個在layout文件中也可以改,不過在application的theme中修改可以應用於所有activity。

⑤ 如何讓android的actionbar浮動且透明

如上圖所示,谷歌地圖的actionbar是透明的,且浮動在整個布局之上,沒有佔用布局空間。其實要做到這樣的效果,我們首先想到的是兩個方面:
1.將讓actionbar浮動起來。
2.給actionbar一個背景,可以為顏色也可以為圖片。
下面我以背景色為顏色舉個例子。
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);ActionBar actionBar = getActionBar();actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#330000ff")));

google的actionbar是分為上下兩欄顯示的,上面的代碼只能設置頂部actionbar的背景色,為了讓下面的背景色一致,還需要添加一行代碼:
actionBar.setSplitBackgroundDrawable(newColorDrawable(Color.parseColor("#330000ff")));

如果我們不想在代碼中設置actionbar背景,而是在style中修改xml,可以自定義actionbar,然後設置下面三個屬性:
<style name="MyActionBar" parent="@android:style/Widget.Holo.ActionBar"> <item name="android:background">@drawable/ab_stacked_solid_inverse_holo</item> <item name="android:backgroundStacked">#346c36</item> <item name="android:backgroundSplit">@drawable/ab_stacked_solid_inverse_holo</item> </style>

其中的background和setBackgroundDrawable對應,backgroundSplit和setSplitBackgroundDrawable對應,而backgroundStacked代表的是actionbar選項卡的背景色。
但是actionbar的背景如果設置為純色,或者為不透明的圖片,那麼上面的方法無法使actionbar達到透明的效果。注意上面的代碼中,我們的顏色值並不是純色的而是有一定的透明度。#330000ff的前兩位代表透明度。

⑥ 安卓自定義actionbar,怎麼設置監聽事件,急急急

用CustomView。

layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:://schemas。android。com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#FFF"
android:text="我才是標題" />

</LinearLayout>
部分代碼:

View customView = LayoutInflater.from(this).inflate(R.layout.back, new LinearLayout(this), false);
getActionBar().setDisplayShowCustomEnabled(true);
getActionBar().setCustomView(customView);
customView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "....", 10000).show();
}
});
我自己測試過,暫無出現問題。

⑦ 如何自定義ActionBar中Tab的高度

解決方案 »

ActionBar的高度是在framework的樣式中定義的
<style name="Widget.ActionBar">
<item name="android:background">@android:drawable/action_bar_background</item>
<item name="android:displayOptions">useLogo|showHome|showTitle</item>
<item name="android:divider">@android:drawable/action_bar_divider</item>
<item name="android:height">?android:attr/actionBarSize</item>
<item name="android:paddingLeft">0dip</item>
<item name="android:paddingTop">0dip</item>
<item name="android:paddingRight">0dip</item>
<item name="android:paddingBottom">0dip</item>
<item name="android:titleTextStyle">@android:style/TextAppearance.Widget.ActionBar.Title</item>
<item name="android:subtitleTextStyle">@android:style/TextAppearance.Widget.ActionBar.Subtitle</item>
<item name="android:progressBarStyle">@android:style/Widget.ProgressBar.Horizontal</item>
<item name="android:indeterminateProgressStyle">@android:style/Widget.ProgressBar.Small</item>
<item name="android:homeLayout">@android:layout/action_bar_home</item>如果想修改,可以自定義樣式,來繼承ActionBar的樣式;或者在當前主題中修改


ActionBar的系統樣式在framework/base/core/res/res/values/styles里有定義
系統定義了ActionBar的高度<item name="android:height">?android:attr/actionBarSize</item>如果想修改,可以考慮自定義一個樣式,繼承系統的ActionBarStyle,自定義高度又或者在當前的主題中修改

⑧ 自定義的actionbar怎麼實現透明度漸變

public class FadingActionBarHelper {

private static final String TAG = "FadingActionBarHelper";

private int mAlpha = 255;
private Drawable mDrawable;
private boolean isAlphaLocked;

private final ActionBar mActionBar;

public FadingActionBarHelper(final ActionBar actionBar) {
mActionBar = actionBar;
}

public FadingActionBarHelper(final ActionBar actionBar, final Drawable drawable) {
mActionBar = actionBar;
(drawable);
}

public void (Drawable drawable) {
(drawable, true);
}

@TargetApi(Build.VERSION_CODES.KITKAT)
public void (Drawable drawable, boolean mutate) {
mDrawable = mutate ? drawable.mutate() : drawable;
mActionBar.setBackgroundDrawable(mDrawable);
if (mAlpha == 255) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
mAlpha = mDrawable.getAlpha();
} else {
setActionBarAlpha(mAlpha);
}
}

/**
* An {@link android.app.ActionBar} background drawable.
*
* @see #(android.graphics.drawable.Drawable)
* @see #setActionBarAlpha(int)
*/
public Drawable () {
return mDrawable;
}

/**
* Please use this method for global changes only!
* This is helpful when you need to provide something like
* Navigation drawer: lock ActionBar and set
* {@link android.graphics.drawable.Drawable#setAlpha(int)}
* to {@link #()} directly.
*
* @param alpha a value from 0 to 255
* @see #()
* @see #getActionBarAlpha()
*/
public void setActionBarAlpha(int alpha) {
if (mDrawable == null) {
Log.w(TAG, "Set action bar background before setting the alpha level!");
return;
}
if (!isAlphaLocked) {
mDrawable.setAlpha(alpha);
View view = mActionBar.getCustomView();
if(view!=null){
//這里是對自定義actionbar背景的處理,我這邊就草草了事了
if(alpha>=55){
view.findViewById(R.id.search_button).setBackgroundResource(R.drawable.search);
view.findViewById(R.id.refresh_button).setBackgroundResource(R.drawable.refresh);
}else{
view.findViewById(R.id.search_button).setBackgroundResource(R.drawable.skin_nav_icon_l_search_rev);
view.findViewById(R.id.refresh_button).setBackgroundResource(R.drawable.skin_nav_icon_r_refresh_rev);
}
Log.i(TAG, "search_button.alpha=>"+alpha);
}
}
mAlpha = alpha;
}

public int getActionBarAlpha() {
return mAlpha;
}

/**
* When ActionBar's alpha is locked {@link #setActionBarAlpha(int)}
* won't change drawable\'s alpha (but will change {@link #getActionBarAlpha()} level)
*
* @param lock
*/
public void setActionBarAlphaLocked(boolean lock) {

// Update alpha level on unlock
if (isAlphaLocked != (isAlphaLocked = lock) && !isAlphaLocked) {
setActionBarAlpha(mAlpha);
}
}

public boolean isActionBarAlphaLocked() {
return isAlphaLocked;
}
}

2.其他的組件類我這不了,有興趣的朋友自行下載github上的項目吧o(∩_∩)o。

[java] view plain
public class NotifyingScrollView extends ScrollView {
// Edge-effects don't mix well with the translucent action bar in Android 2.X
private boolean mDisableEdgeEffects = true;

/**
* @author Cyril Mottier
*/
public interface OnScrollChangedListener {
void onScrollChanged(ScrollView who, int l, int t, int oldl, int oldt);
}

private OnScrollChangedListener mOnScrollChangedListener;

public NotifyingScrollView(Context context) {
super(context);
}

public NotifyingScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}

public NotifyingScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (mOnScrollChangedListener != null) {
mOnScrollChangedListener.onScrollChanged(this, l, t, oldl, oldt);
}
}

public void setOnScrollChangedListener(OnScrollChangedListener listener) {
mOnScrollChangedListener = listener;
}

@Override
protected float getTopFadingEdgeStrength() {
// http://stackoverflow.com/a/6894270/244576
if (mDisableEdgeEffects && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return 0.0f;
}
return super.getTopFadingEdgeStrength();
}

@Override
protected float getBottomFadingEdgeStrength() {
// http://stackoverflow.com/a/6894270/244576
if (mDisableEdgeEffects && Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
return 0.0f;
}
return super.getBottomFadingEdgeStrength();
}
}

⑨ android 自定義控制項中怎麼隱藏actionbar

1.在配置文件中通過android:theme=""屬性隱藏:
<activity
android:name=".MainActivity"
android:label="@string/app_name"
<!-- 這行代碼便可以隱藏ActionBar -->
android:theme="@android:style/Theme.Light.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

2.在Activity代碼中隱藏:
public class MainActivity extends Activity {

ActionBar actionBar; //聲明ActionBar

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

actionBar = getActionBar(); //得到ActionBar
actionBar.hide(); //隱藏ActionBar
}
}

⑩ 開發產品中是ActionBar居多還是自定義標題欄居多

使用自定義ActionBar就可以 Android系統中ActionBar默認的布局不美觀且難於控制,通過為ActionBar自定義布局的方式可以靈活控制ActionBar。

熱點內容
文件des加密 發布:2024-04-24 19:24:20 瀏覽:704
魔獸世界data文件夾 發布:2024-04-24 19:24:13 瀏覽:213
蘋果手機怎麼清空緩存 發布:2024-04-24 19:23:38 瀏覽:892
微信密碼沒有手機號如何找回 發布:2024-04-24 19:18:20 瀏覽:874
微雲解析源碼 發布:2024-04-24 19:13:58 瀏覽:791
紙張記錄存儲 發布:2024-04-24 19:00:35 瀏覽:800
存儲結構中定義一個集合 發布:2024-04-24 18:52:05 瀏覽:427
搜狗網址存儲的密碼在哪裡查看 發布:2024-04-24 18:52:03 瀏覽:917
抖音搶福袋腳本最新 發布:2024-04-24 18:52:00 瀏覽:701
畫機械3D圖電腦要什麼配置 發布:2024-04-24 18:50:24 瀏覽:393