簡體   English   中英

標簽內的android工具欄

[英]android toolbar inside a tab

我想在我的一個標簽內制作一個工具欄,但對我沒有任何作用。 可以做到嗎? 我目前在標簽上方有一個工具欄,但我需要它顯示在第一個標簽內。

這是代碼(選項卡上方的工具欄):

import ...

public class MainActivity extends ActionBarActivity {

Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;
CharSequence Titles[] = {"Projects", "People", "Files"};
int Numboftabs = 3;

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

    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);

    adapter = new ViewPagerAdapter(getSupportFragmentManager(), Titles, Numboftabs);
    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);
    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); 
    tabs.setViewPager(pager);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.menu_primary, menu);
    return true;
}

final ArrayList<String> listItems = new ArrayList<String>();

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.addButton) {
        final TextView noProject = (TextView) findViewById(R.id.NOPROJECT);
        final ListAdapter addAdapter = new ArrayAdapter<String>(this,
                R.layout.list_item, R.id.listFrame, listItems);
        final ListView lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(addAdapter);

        noProject.setVisibility(View.GONE);
        lv.setVisibility(View.VISIBLE);
        listItems.add("New Project");

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent switchToEdit = new Intent(MainActivity.this,
                        TeamCreate.class);
                startActivity(switchToEdit);
            }
        });
    }
    return super.onOptionsItemSelected(item);
}
}

第一個標簽:

import ...

public class Tab1 extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View v =inflater.inflate(R.layout.tab_1,container,false);
    return v;
}
}

我坦率地說,使用非標准的工具欄和選項卡也面臨類似的挑戰,盡管結果看起來很奇怪。 您可能可以使用類似的技術來使工具欄在選項卡下工作,但是這可能會使用戶撓頭。 您的技術將有2個工具欄,一個工具欄包含TabLayout小部件,另一個工具欄位於ViewPager中的片段內。

我在應用程序的“詳細信息”視圖中使用了“材料設計”選項卡。 直到我為平板電腦制作了2窗格的Master-Detail視圖,它看起來還不錯。 然后,我遇到了一個問題,這些選項卡希望直接位於工具欄下方,但是沒有一種明顯的方法可以使“主控和詳細信息”共享工具欄,而這些選項卡僅顯示在屏幕的“詳細信息”部分下方。

我的工作原理是:

  1. 在styles.xml中創建一個“ NoActionBar”樣式,然后使用“ android:theme =“ @ style / AppTheme”在您的主布局中引用它。

     <!-- Turn off normal application bar so we can put our toolbars where we please. Do this by replacing "Theme.AppCompat.Light.DarkActionBar" with NoActionBar per http://www.truiton.com/2015/04/android-action-bar-dialog-using-toolbar/ --> <style name="AppTheme" parent="MaterialTheme.Base"/> <style name="MaterialTheme.Base" parent="Theme.AppCompat.Light.NoActionBar"> <item name="windowNoTitle">true</item> <!--We will be using the toolbar so no need to show ActionBar--> <item name="windowActionBar">false</item> 
  2. 扔android.support.design.widget.CoordinatorLayout並只使用LinearLayout。

  3. 這是一個包含工具欄的代碼段,其中的選項卡僅占用部分屏幕空間,而不是整個寬度。 請注意,工具欄具有“ app:layout_scrollFlags =“ scroll | enterAlways”:

      <FrameLayout android:id="@+id/detail_container" android:layout_width="0dp" android:layout_height="match_parent" android:layout_gravity="end" android:layout_weight="2" tools:name="com.android.bazemom.popularmovies.TabContainerFragment" > <!-- second toolbar that will be used for the tabs --> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorPrimary" android:minHeight="?attr/actionBarSize" app:layout_scrollFlags="scroll|enterAlways" /> <android.support.design.widget.TabLayout android:id="@+id/tabanim_tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <!-- Helps handing the Fragments to load for each Tab --> <android.support.v4.view.ViewPager android:id="@+id/tabanim_viewpager" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 

這是寬平板電腦上的主從視圖的外觀。 選項卡以綠色圈出。 選項卡上方是帶有操作的工具欄。 平板電腦的寬屏屏幕截圖,僅使用部分屏幕寬度顯示標簽。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM