如何在Android中正确设置URL图像?

时间:2017-01-14 07:13:00

标签: android image

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

我不知道为什么我的图像会向上移动。图像应该像这样呈现:

enter image description here

我的图片呈现如下:

enter image description here

这是我的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

<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

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;
    }
}

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

3 个答案:

答案 0 :(得分:1)

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

之上
SELECT
  e.event_date,
  r1.name as class_40_winner,
  r2.name as class_30_winner
FROM
  events e,
  results r1,
  results r2
WHERE
  r1.class = 40 AND
  r2.class = 30 AND
  r1.position = 1 AND
  r2.position = 1 AND
  r1.result_event = e.id AND
  r2.result_event = e.id

答案 1 :(得分:1)

您必须在布局中包含自定义操作栏。

请关注this链接。它与代码捕捉和深度解释非常好。 :)

答案 2 :(得分:0)

在MainAcitivity中,您应该扩展Activity而不是AppCompatActivity。 AppCompatActivity显示一个栏