如何在Android中正确设置URL图像? [英] How to set URL image properly in Android?

查看:142
本文介绍了如何在Android中正确设置URL图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android的新手,我正在尝试在主屏幕上打开URL图像。我的问题是我在主页上也使用导航抽屉。一切正常,只有来自数据库的主图像略微向上,而我复制的图像看起来很完美。

I am new to android, I am trying to open URL image on my main screen. My problem is that I am using navigation drawer also on my main page. Everything works fine only my main image which is coming from database is going slightly upwards, whereas the example which I copied in that the image is looking perfectly.

我不知道知道为什么我的形象向上发展。图片应如下呈现:

I don't know why my image is going upwards. The image should render like this :

我的图像呈现如下:

< img src =https://i.stack.imgur.com/2TnF2.pngalt =在此处输入图像说明>

这是我的FragmentOne.xml:

Here is my FragmentOne.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:orientation="vertical">

        <LinearLayout
            android:id="@+id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.7">

            <ImageView
                android:id="@+id/ms1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="fitXY"
                android:adjustViewBounds="true"
                android:src="@mipmap/ic_launcher" />


        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="0.3">


            <ImageView
                android:id="@+id/ms2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:id="@+id/ms3"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:scaleType="fitXY"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
    </LinearLayout>

</LinearLayout>

我的containt_main.xml

My containt_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">



    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </FrameLayout>
</RelativeLayout>

我的主要活动:

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import com.example.zeba.broccoli.AddToCart;
import com.example.zeba.broccoli.FragmentOne;
import com.example.zeba.broccoli.FragmentTwo;
import com.example.zeba.broccoli.Login;
import com.example.zeba.broccoli.R;


public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    /**
     * As i told you i just create only navigation drawer with displaying images.
     * I jus created two fragment only for understanding purpose.
     * If you want more then developed by yourself.
     * Remove this comment no need after you understood.
     * @param savedInstanceState
     */

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);



        DrawerLayout 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);

        //add this line to display menu1 when the activity is loaded
        displaySelectedScreen(0);
    }


    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main
                , menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_settings:
                // User chose the "Settings" item, show the app settings UI...


            case R.id.action_cart:


                Intent ibs = new Intent(MainActivity.this,AddToCart.class);
                ibs.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(ibs);
                finish();
                break;
            // User chose the "Favorite" action, mark the current item
            // as a favorite...


            default:
                // If we got here, the user's action was not recognized.
                // Invoke the superclass to handle it.
                return super.onOptionsItemSelected(item);

        }
        return true;
    }
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.br_menu) {
            Intent i = new Intent(MainActivity.this,Login.class);
            startActivity(i);
            displaySelectedScreen(0);
        }
        else if (id == R.id.tr_ordr)
        {
            displaySelectedScreen(1);
        }
        else if (id == R.id.pl_ordr)
        {
            displaySelectedScreen(2);
        } else if (id == R.id.profl)

        {
            displaySelectedScreen(3);
        }
        else if (id == R.id.addr)
        {
            displaySelectedScreen(4);
        } else if (id == R.id.crds)
        {
            displaySelectedScreen(5);
        }
        return true;
    }

    private void displaySelectedScreen(int position)
    {
        try {

            Fragment fragment = null;

            if (position == 0)
            {
                fragment = new FragmentOne();
            }
            else if (position == 1)
            {
                fragment = new FragmentTwo();
            }
            else if (position == 2)
            {

            } else if (position == 3) {

            } else if (position == 4) {

            } else if (position == 5) {

            }

            //replacing the fragment
            if (fragment != null) {
                FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                ft.replace(R.id.content_frame, fragment);
                ft.commit();
            }

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

My FragmentOne.java

My FragmentOne.java

public class FragmentOne extends Fragment
{
    Activity activity;

    View rootView;

    ImageView im1, im2, im3;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        rootView = inflater.inflate(R.layout.fragment_one, container, false);

        activity = getActivity();

        im1 = (ImageView)rootView.findViewById(R.id.ms1);
        im2 = (ImageView)rootView.findViewById(R.id.ms2);

        im2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(getActivity(), Login.class);
                startActivity(intent);
            }
        });






        im3 = (ImageView)rootView.findViewById(R.id.ms3);

        Picasso.with(activity).load("http://i.imgur.com/DvpvklR.png").into(im1);
        Picasso.with(activity).load("https://api.learn2crack.com/android/images/donut.png").into(im2);
        Picasso.with(activity).load("http://hdwallpaperbackgrounds.net/wp-content/uploads/2016/07/hd-nature-wallpapers.jpg").into(im3);

        return rootView;
    }
}

我试过做调试但是也没用,就是如此奇怪的问题。

I tried doing debugging but then also no use, it's so weird problem.

推荐答案

main_content xml出现问题。当您在 main_activity xml中使用它时,您的图像会被切断。使用这个技巧它会起作用。简单的看跌保证金高于 main_content xml

The problem occur for main_content xml . your image is cut off when you use it in you main_activity xml . Use this trick it will work . Simple put margin above your main_content xml

<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <FrameLayout
        android:layout_marginTop="?actionBarSize"
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </FrameLayout>
</RelativeLayout>

这篇关于如何在Android中正确设置URL图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆