简体   繁体   English

如何使用片段自定义标题栏和选项卡

[英]How to make Title Bar & Tabs custom using Fragments

在此处输入图片说明

I am writing a program in which i am using Fragments to work with Tabs, but i need few changes in my UI 我正在编写一个程序,其中我使用片段来与Tabs一起工作,但是我的UI几乎不需要更改

I need few changes in my UI , small 5 questions: 我需要在UI中进行 一些更改仅需 5个问题:

Question 1: How to Change Background color of TITLE BAR (I want RED) 问题1:如何更改标题栏(我要红色)的背景颜色

Question 2: How to Change Background color of Tab (I want GRAY) 问题2:如何更改选项卡的背景色(我想要灰色)

Question 3: How to Change color of Selected Tab (I want RED) 问题3:如何更改选定标签的颜色(我要红色)

Question 4: How to show Icon above the Text in Tabs 问题4:如何在标签中的文本上方显示图标

Question 5: How to place share button over Title Bar at Right side 问题5:如何在右侧的标题栏上放置共享按钮

Manifest.xml: 的Manifest.xml:

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.test.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

MainActivity.java: MainActivity.java:

 public class MainActivity extends FragmentActivity {

        ViewPager ViewPager;
        TabsAdapter TabsAdapter;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);         

            //create a new ViewPager and set to the pager we have created in Ids.xml
            ViewPager = new ViewPager(this);
            ViewPager.setId(R.id.pager);
            setContentView(ViewPager);


            //Create a new Action bar and set title to strings.xml
            final ActionBar bar = getActionBar();
            bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
            bar.setTitle(R.string.app_name);

            //Attach the Tabs to the fragment classes and set the tab title.
            TabsAdapter = new TabsAdapter(this, ViewPager);

            TabsAdapter.addTab(bar.newTab().setText("About"),
                    FragAbout.class, null);
            TabsAdapter.addTab(bar.newTab().setText("Location"),
                    FragLocation.class, null);
            TabsAdapter.addTab(bar.newTab().setText("Menus"),
                    FragMenus.class, null);
            TabsAdapter.addTab(bar.newTab().setText("Reservation"),
                    FragReservation.class, null);
            TabsAdapter.addTab(bar.newTab().setText("Social"),
                    FragSocial.class, null);

            if (savedInstanceState != null) {
                bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
            }

        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
        }

        // create TabsAdapter to create tabs and behavior
        public static class TabsAdapter extends FragmentPagerAdapter
        implements ActionBar.TabListener, ViewPager.OnPageChangeListener {

            private final Context mContext;
            private final ActionBar mActionBar;
            private final ViewPager mViewPager;
            private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

            static final class TabInfo {
                private final Class<?> clss;
                private final Bundle args;

                TabInfo(Class<?> _class, Bundle _args) {
                    clss = _class;
                    args = _args;
                }
            }

            public TabsAdapter(FragmentActivity activity, ViewPager pager) {
                super(activity.getSupportFragmentManager());
                mContext = activity;
                mActionBar = activity.getActionBar();
                mViewPager = pager;
                mViewPager.setAdapter(this);
                mViewPager.setOnPageChangeListener(this);
            }

            public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args) {
                TabInfo info = new TabInfo(clss, args);
                tab.setTag(info);
                tab.setTabListener(this);
                mTabs.add(info);
                mActionBar.addTab(tab);
                notifyDataSetChanged();             
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // TODO Auto-generated method stub
            }   

            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                mActionBar.setSelectedNavigationItem(position);
            }

            @Override
            public void onTabReselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }

            @Override
            public void onTabSelected(Tab tab, FragmentTransaction ft) {
                Object tag = tab.getTag();
                for (int i=0; i<mTabs.size(); i++) {
                    if (mTabs.get(i) == tag) {
                        mViewPager.setCurrentItem(i);
                    }
                }
            }

            @Override
            public void onTabUnselected(Tab tab, FragmentTransaction ft) {
                // TODO Auto-generated method stub
            }

            @Override
            public Fragment getItem(int position) {
                TabInfo info = mTabs.get(position);
                return Fragment.instantiate(mContext, info.clss.getName(), info.args);
            }

            @Override
            public int getCount() {
                return mTabs.size();
            }

        }

FragAbout.java: FragAbout.java:

public class FragAbout extends Fragment {

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {

            View myFragmentView = inflater.inflate(R.layout.frag_about, container, false);
            return myFragmentView;

    } 

}

use this for QA:2,3,4 将其用于质量检查:2、3、4

create xml(tab1) in drawble folder 在drawble文件夹中创建xml(tab1)

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:drawable="@drawable/videos_gray"
      android:state_selected="true" />
<!-- When not selected, use white-->
<item android:drawable="@drawable/videos_white" />

and use that in java file 并在java文件中使用

 // Create an actionbar
    ActionBar actionBar = getActionBar();

    // Hide Actionbar Icon
    actionBar.setDisplayShowHomeEnabled(false);

    // Hide Actionbar Title
    actionBar.setDisplayShowTitleEnabled(false);

    // Create Actionbar Tabs
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Create first Tab
    tab = actionBar.newTab().setTabListener(new FragmentTab1());
    // Create your own custom icon
    tab.setIcon(R.drawable.tab1);
    actionBar.addTab(tab);

    // Create Second Tab
    tab = actionBar.newTab().setTabListener(new FragmentTab2());
    // Set Tab Title
    tab.setText("Tab2");
    actionBar.addTab(tab);

    // Create Third Tab
    tab = actionBar.newTab().setTabListener(new FragmentTab3());
    // Set Tab Title
    tab.setText("Tab3");
    actionBar.addTab(tab);

I think you already got the answer, that's why you developed this: 我认为您已经找到了答案,这就是您开发此答案的原因:

https://stackoverflow.com/questions/19000981/actionbarsherlock-swapping-tabs-using-viewpager https://stackoverflow.com/questions/19000981/actionbarsherlock-swapping-tabs-using-viewpager

Anyways if you still looking for answer, So 无论如何,如果您仍在寻找答案,那么

I really recommend you to use ActionBarStyleGenerator : 我真的建议您使用ActionBarStyleGenerator

http://jgilfelt.github.io/android-actionbarstylegenerator/ http://jgilfelt.github.io/android-actionbarstylegenerator/

With that tool you can easily theme your graphic elements in your toolbar. 使用该工具,您可以轻松地在工具栏中为图形元素设置主题。

Ans for Q.1 : You can make a new layout and set its Height at desired "dp" and change the background of that layout to Red color (use resource for that - preferably define a color using color code OR bring in image with Red background). 对Q.1的回答:您可以制作一个新的布局,并将其高度设置为所需的“ dp”,然后将该布局的背景更改为红色(为此使用资源-最好使用颜色代码定义颜色,或使用红色导入图像背景)。 This does not involve using fragments. 这不涉及使用片段。

Ans for Q.5: 问题5:

If you use my option as above, use RelatieLayout for Title bar. 如果您如上所述使用我的选项,请为标题栏使用RelatieLayout。 Then position the button you want at the rightmost side.(Use gravity or manually drag or android:layout_alignParentRight="true" - whichever way you want) 然后将您想要的按钮放在最右侧。(使用重力或手动拖动或android:layout_alignParentRight =“ true”-任意一种方式)

Can't say anything about other questions. 关于其他问题无话可说。 Hope this helps 希望这可以帮助

Some other Links: 其他一些链接:

How do I change the background of an Android tab widget? 如何更改Android标签小部件的背景?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM