如何从导航抽屉 Activity 中的 URL 将图像添加到弹出窗口 window
How to add image to popup window from URL in navigation drawer Activity
我在主导航抽屉 activity 中实现了弹出窗口 window。我想要的是从 URL 获取图像,在 imageView 中设置该图像
activity_pop_up.xml,并在我的 activity
中显示弹出窗口 window
这是我的 MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// HERE IS POPUP WINDOW
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_pop_up, null);
pw = new PopupWindow(layout, 600, 800, true);
findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 0, 0);
}
});
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();
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().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;
}
if (id == R.id.galerija)
{
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
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");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
activity_pop_up.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/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">
<ImageView
android:id="@+id/baner3img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/autojedan" />
</RelativeLayout>
@Nikola 您可以创建一个 class,它使用您的 activity_pop_up 具有 imageView 的自定义布局扩展 Dialog。您可以设置 requestWindowFeature(Window.FEATURE_NO_TITLE) 使其看起来像弹出窗口。对于图片下载,您可以使用 Picasso、Universal Image Loader 或 Glide 并将其显示在您的 UI.
我在主导航抽屉 activity 中实现了弹出窗口 window。我想要的是从 URL 获取图像,在 imageView 中设置该图像 activity_pop_up.xml,并在我的 activity
中显示弹出窗口 window这是我的 MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// HERE IS POPUP WINDOW
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_pop_up, null);
pw = new PopupWindow(layout, 600, 800, true);
findViewById(R.id.relativeLayoutZaFragment).post(new Runnable() {
public void run() {
pw.showAtLocation(findViewById(R.id.relativeLayoutZaFragment), Gravity.CENTER, 0, 0);
}
});
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();
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().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;
}
if (id == R.id.galerija)
{
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
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");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
activity_pop_up.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/popup"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bolt.automagazin.PopUp">
<ImageView
android:id="@+id/baner3img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
app:srcCompat="@drawable/autojedan" />
</RelativeLayout>
@Nikola 您可以创建一个 class,它使用您的 activity_pop_up 具有 imageView 的自定义布局扩展 Dialog。您可以设置 requestWindowFeature(Window.FEATURE_NO_TITLE) 使其看起来像弹出窗口。对于图片下载,您可以使用 Picasso、Universal Image Loader 或 Glide 并将其显示在您的 UI.