Skip to content

Instantly share code, notes, and snippets.

@yukpiz
Last active July 8, 2016 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yukpiz/2b27a6ca39d55ab48e0e to your computer and use it in GitHub Desktop.
Save yukpiz/2b27a6ca39d55ab48e0e to your computer and use it in GitHub Desktop.
Android ActionBar

Android ActionBar.


ActionBarを画面下部へ表示する


/*
 * ActionBarを画面下部に移動
 */
ActionBar actionBar = this.getActionBar();
actionBar.setDisplayShowTitleEnabled(false); //ActionBarのアプリタイトルを非表示
actionBar.setDisplayShowHomeEnabled(false); //ActionBarのアプリアイコンを非表示

//クラス名でViewを検索できるクラスをローカルクラスとして定義
class FindClass {

  public List<View> findViewsWithClassName(View v, String className) {

    List<View> views = new ArrayList<View>();
    findViewsWithClass(v, className, views);
    return views;

  }

  private <T extends View> List<T> findViewsWithClass(View v, Class<T> clazz) {

    List<T> views = new ArrayList<T>();
    findViewsWithClass(v, clazz.getName(), views);
    return views;

  }

  private <T extends View> void findViewsWithClass(View v, String clazz, List<T> views) {

    if (v.getClass().getName().equals(clazz)) {

      views.add((T) v);

    }

    if (v instanceof ViewGroup) {

      ViewGroup g = (ViewGroup) v;

      for (int i = 0; i < g.getChildCount(); i++) {

        findViewsWithClass(g.getChildAt(i), clazz, views);

      }
    }
  }
}

FindClass findClass = new FindClass(); //上記のローカルクラスをインスタンス化
String barClazz = "com.android.internal.widget.ActionBarContainer"; //ActionBarのクラス名
View root = this.getWindow().getDecorView(); //Activityの最上位のViewを取得
View firstChild = ((ViewGroup) root).getChildAt(0); //rootの1つ目の子要素を取得

if (firstChild instanceof ViewGroup) { //firstChildがViewGroupと判定されれば

  ViewGroup viewGroup = (ViewGroup) firstChild; //ViewGroup型変数へキャスト
  List<View> views = findClass.findViewsWithClassName(root, barClazz); //新しくViewの配列を定義

  //取得したActionBar要素を一度Viewより削除して再度末尾へ追加する手法
  if (!views.isEmpty()) {

    for (View vv : views) {

      viewGroup.removeView(vv);

    }

    for (View vv : views) {

      viewGroup.addView(vv);

    }
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment