半透明图像滑块与自定义ActionBar

时间:2014-12-14 08:45:19

标签: android xml android-layout

我正在尝试使用具有以下功能的PageAdapter获取ImageSlider活动: -

  1. 半透明背景。
  2. 半透明自定义操作栏,包含TextView和ImageButtons。
  3. 我面临的问题: -

    1. 为了使活动半透明,我将android:theme="@android:style/Theme.Translucent添加到清单<activity> XML布局文件中的android:background="#20000000"。 但是,在getActionBar()方法中执行此操作onCreate()后,返回 null 。 如果我删除android:theme="@android:style/Theme.Translucent,则该活动不再是半透明的,但getActionBar()现在正在运作。
    2. 我正在使用getActionBar()为自定义半透明操作栏充气,所以到目前为止,我可以获得半透明活动或自定义操作栏,而不是同时使用它们。

        我的自定义操作栏中的
      1. TextView 用于显示当前图像,不包括总图像数量,例如 1/5 ,向右滑动以获得 2/5 ,依此类推。
      2. 但有时它显示正确,有时只是随机数字。

        我的代码: -

        TextView header;
        int count=0;
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        
            //Getting Action Bar
            ActionBar mActionBar= getActionBar();
        
            LayoutInflater minflate =(LayoutInflater) mActionBar.getThemedContext().getSystemService(LAYOUT_INFLATER_SERVICE);
             mview = minflate.inflate(R.layout.albumactionbar,null);
        
            mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM,ActionBar.DISPLAY_SHOW_CUSTOM|ActionBar.DISPLAY_SHOW_HOME|ActionBar.DISPLAY_SHOW_TITLE);
            mActionBar.setCustomView(mview, new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
        
            header =(TextView)mview.findViewById(R.id.action_text);
        
            setContentView(R.layout.albummainview);
            }
        

        albumactionbar.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="wrap_content"
            android:background="@drawable/custom_ab"
            android:orientation="horizontal"
             >
            <ImageButton 
                android:id="@+id/album_back"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_menu_back"
                android:layout_alignParentLeft="true"
                android:background="@color/Translucent"
                android:layout_gravity="center"
                />
            <TextView 
                android:id="@+id/action_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#ffffff"
                android:layout_gravity="center"
                android:text="Gallery Count"
                android:textSize="18sp"
                android:layout_centerInParent="true"/>
            <ImageButton 
                android:id="@+id/album_after"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_menu_after"
                android:layout_alignParentRight="true"
                android:background="@color/Translucent"
                android:layout_gravity="center"
                />
        
        
        </RelativeLayout>
        

        custom_ab.xml: -

        <?xml version="1.0" encoding="utf-8"?>
        <shape xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="rectangle" >
        
        
        
            <gradient
                android:startColor="#FF000000"
                android:endColor="#00000000" 
                android:angle="270"
                />
        
        </shape>
        

        现在在 MyAdpater 中,我正在更改自定义操作栏的header TextView 值: -

        public class AlbumAdapter extends PagerAdapter
        
            {
        
                Context context;
        
                public AlbumAdapter(Context context){
                    this.context = context;
        
                }
        
        
                @Override
                public Object instantiateItem(ViewGroup container, int position) {
                    // TODO Auto-generated method stub
        
                    LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        
                    View viewItem = inflater.inflate(R.layout.albumitemview, container, false);
        
                    //header is the custom action bar's text view.
                    header.setText((position+1)+" out of "+count);
        
                    .....
        
        }
        }
        

        所以请指导我解决这两个问题,关于问题2,是否是改变textView值的正确位置?

1 个答案:

答案 0 :(得分:1)

第一个问题是,你可以用<color name="Transparent_Black">#50000000</color>在styles.xml中更改操作栏的颜色,然后在你的活动中添加getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY); 的onCreate()即可。我用过这个,工作正常。执行此操作时,您可以按标题getActionBar().setTitle("")添加标题。

对于您的第二个问题,您只需要在 ViewPager 中添加一个ViewPager.OnPageChangeListener方法onPageSelected(position),就可以更新header。< / p>

因此,如果两个问题都已解决,您可以将getActionBar().setTitle((position+1)+" out of "+count)添加到onPageSelected(position)方法中。