android - 带有自定义 ActionBar 的半透明 ImageSlider

标签 android xml android-layout

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

  1. 半透明背景。
  2. 带有 TextView 和 ImageButtons 的半透明自定义操作栏。

我面临的问题:-

  1. 为了使 Activity 半透明,我添加了 android:theme="@android:style/Theme.Translucent<activity> list android:background="#20000000"XML 布局 文件中。 但是在这样做之后 getActionBar()在我的 onCreate()方法返回 null。 如果我删除 android:theme="@android:style/Theme.Translucent然后 Activity 不再是半透明的而是getActionBar()现在正在工作。

我正在使用 getActionBar()膨胀一个自定义的半透明操作栏,所以现在我可以获得半透明 Activity 或一个自定义操作栏,而不是同时获得它们。

    我的自定义操作栏中的
  1. TextView 用于显示当前图像中的所有图像,例如 1 out of 5 并向右滑动以获得 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 值的正确位置吗?

最佳答案

对于第一个问题,您可以通过 <color name="Transparent_Black">#50000000</color> 在 styles.xml 中更改 actionbar 的颜色。例如,然后添加 getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);在你的 Activity 中 onCreate()。我用过这个,效果很好。当你这样做时,你可以通过 getActionBar().setTitle("") 添加标题相反,如果标题。

对于你的第二个问题,你只需要添加一个 ViewPager.OnPageChangeListener到你的 ViewPager,它有一个方法 onPageSelected(position) ,您可以在那里更新您的 header .

所以如果两个问题都解决了你可以添加getActionBar().setTitle((position+1)+" out of "+count)给你的onPageSelected(position)方法。

关于android - 带有自定义 ActionBar 的半透明 ImageSlider,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467703/

相关文章:

java - 在 Android Manifest.xml 中接收短信

c++ - 安装qt中的./configure步骤

android - android布局中的错误定位

android - 在 ListView 中最右边对齐复选框

android - Google 开发者帐户(发布者)

android - 在启动 Activity 时在没有任何数据的情况下初始化 MPAndroidChart

java - YouTubePlayerView 画中画 android

java - 从 XML 或 JSON 反序列化不受信任的数据

android - 背景有问题 :@drawable/img

安卓异步任务