Android 는 Action Bar 를 교묘 하 게 사용 하여 드 롭 다운 내 비게 이 션 을 실현 합 니 다.
Actionbar 를 이용 하여 드 롭 다운 네 비게 이 션 방식 을 쉽게 실현 할 수 있 습 니 다.이런 효 과 를 실현 하려 면:
1)actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST)
2)setListNavigationCallbacks(SpinnerAdapter adapter,ActionBar.OnNavigationListener callback).
우선 Fragment 클래스 를 만 듭 니 다:
package ccom.app.main;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.TextView;
@SuppressLint("NewApi")
public class MyFragment extends Fragment {
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) {
  Context context = this.getActivity();
  TextView tv = new TextView(context);
  Bundle arc = this.getArguments();
  int tabs=arc.getInt("key");
  
  tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));
  tv.setText("hello actionbar "+tabs);
  return tv;
 }
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 tools:context=".Main" >
 <LinearLayout
  android:id="@+id/content"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/text1"
 android:textColor="#fff"
 android:background="#696969"
 android:layout_width="60sp"
 android:layout_height="match_parent"
 android:textAppearance="?android:attr/textAppearanceListItemSmall"
 android:gravity="center_vertical"
 android:paddingStart="?android:attr/listPreferredItemPaddingStart"
 android:minHeight="?android:attr/listPreferredItemHeightSmall"
/>
package ccom.app.main;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.ArrayAdapter;
@SuppressLint("NewApi")
public class Main extends Activity implements ActionBar.OnNavigationListener {
 ActionBar actionBar = null;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  actionBar = this.getActionBar();
  actionBar.setDisplayShowTitleEnabled(true);
  actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
  actionBar.setListNavigationCallbacks(new ArrayAdapter(Main.this,
    R.layout.mytextview, R.id.text1, new String[] { "tab1", "tab2",
      "tab3" }), this);
 }
 @Override
 public boolean onNavigationItemSelected(int itemPosition, long itemId) {
  MyFragment mf = new MyFragment();
  Bundle bundle = new Bundle();
  bundle.putInt("key", itemPosition + 1);
  mf.setArguments(bundle);
  FragmentTransaction action = this.getFragmentManager()
    .beginTransaction();
  action.replace(R.id.content, mf);
  action.commit();
  return true;
 }
}
 
 이상 이 바로 본문의 전체 내용 입 니 다.여러분 께 참고 가 될 수 있 기 를 바 랍 니 다.여러분 들 도 저 희 를 많이 응원 해 주시 기 바 랍 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Action Bar에는 항상 메뉴의 "·"을, 하위 메뉴에는 아이콘을 표시합니다.Action BarSherlock과 Support Library의 Action BarActivity를 사용하면 안드로이드 2.x도 액션 바가 있는 액티비티를 만들 수 있지만, 물리적 메뉴 키가 있는 터미널은 액션 바...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.