弹出窗口作为导航抽屉活动中的背景图像运行



[图像示例] [1]我在导航抽屉活动中实现了弹出屏幕。问题是弹出窗口显示的是活动的背景图像。当活动开始并在3秒后解散后,我想弹出的内容可在前景中看到。

这是我的活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();    
    try {
        LayoutInflater inflater1 = (LayoutInflater) MainActivity.this
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // Inflate the view from a predefined XML layout
        View layout = inflater1.inflate(R.layout.activity_pop_up,
                (ViewGroup) findViewById(R.id.relativeLayoutZaFragment));
        // create a 300px width and 470px height PopupWindow
        pw = new PopupWindow(layout, 300, 470, true);
        // display the popup in the center
        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
    } catch (Exception e) {
        e.printStackTrace();
    }
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    FragmentMainPage pocetnaFragment = new FragmentMainPage();
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.relativeLayoutZaFragment, pocetnaFragment, "1");
    fragmentTransaction.commit();
    getSupportActionBar().setTitle("Auto magazin");
    ActionBar.LayoutParams p = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    p.gravity = Gravity.LEFT;
    view = getLayoutInflater().inflate(R.layout.action_bar, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.LEFT);
    TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
    Title.setText("Auto magazin");
    getSupportActionBar().setCustomView(view,params);
    getSupportActionBar().setDisplayShowCustomEnabled(true); //show custom title
    getSupportActionBar().setDisplayShowTitleEnabled(false); //hide the default title
}
boolean doubleBackToExitPressedOnce = false;
@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    assert drawer != null;
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
        return;
    }
    else if (doubleBackToExitPressedOnce) {
        super.onBackPressed();
        return;
    }
    this.doubleBackToExitPressedOnce = true;
    Toast.makeText(this, "Pritisnite još jednom za izlaz", Toast.LENGTH_SHORT).show();
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            doubleBackToExitPressedOnce = false;
        }
    }, 2000);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    //noinspection SimplifiableIfStatement
    if (id == R.id.podesavanja)
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item)   {
    int id = item.getItemId();
    if (id == R.id.pocetna)       {
        FragmentMainPage fragmentMainPage = new FragmentMainPage() ;
        FragmentManager manager = getSupportFragmentManager();
        manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
               , fragmentMainPage, fragmentMainPage.getTag()).addToBackStack(null).commit();
        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Auto magazin");
    }
    else if (id == R.id.info)   {
         FragmentMenuPages fragmentMenuPages = new FragmentMenuPages();
         FragmentManager manager = getSupportFragmentManager();
         manager.beginTransaction().replace(R.id.relativeLayoutZaFragment
                , fragmentMenuPages, fragmentMenuPages.getTag()).commit();
        TextView Title = (TextView) view.findViewById(R.id.actionbar_title);
        Title.setText("Info");
        Bundle bundle = new Bundle();
        bundle.putString("href","http://www.magazinauto.com/category/info/");
        bundle.putString("title", "Info");
        fragmentMenuPages.setArguments(bundle);
    }
    else {
        return super.onOptionsItemSelected(item);
    }
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
    }
}

activity_pop_up:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">
  <ImageView
   android:id="@+id/imageView3"
   android:layout_width="match_parent"
   android:layout_height="100dp"
   android:layout_alignParentStart="true"
   android:layout_centerVertical="true"
   app:srcCompat="@drawable/autojedan" />
</RelativeLayout>

content_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayoutZaFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.bolt.automagazin.MainActivity"
tools:showIn="@layout/app_bar_main"
>

您所看到的实际上不是您的PopupWindowinflate()调用将膨胀的View直接连接到relativeLayoutZaFragment,并且Fragment在其顶部添加。然后,由于该View已经连接到父母,因此PopupWindow正在抛出异常,并且永远不会显示。

inflate()中的第二个参数更改为null

View layout = inflater1.inflate(R.layout.activity_pop_up, null);

并将Activity中的CC_11传递给showAtLocation(),而不是夸大的布局:

pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment‌​), Gravity.CENTER, 0, 0);

此外,由于您在onCreate()中进行此操作,因此您需要暂时显示PopupWindow,否则您将获得WindowManager$BadTokenException。您可以通过将showAtLocation()调用放入Runnable中,然后将其发布到View。例如:

final View rl = findViewById(R.id.relativeLayoutZaFragment‌);
rl.post(new Runnable() {
    @Override
        public void run() {
            pw.showAtLocation(rl, Gravity.CENTER, 0, 0);
        }
    }
);

最新更新