简体   繁体   中英

Why the image view android:layout_below is not working

In this page, the @+id/photo_area is dynamic generate as I would like the width align to height , so I have write the following 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="match_parent"
    android:background="@drawable/share_bg" >

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="20dp"
        android:layout_marginTop="20dp"
        android:overScrollMode="never" >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <ImageView
                android:id="@+id/photo_area"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/share_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/photo_area"
                android:layout_above="@+id/share_content"
                android:layout_marginLeft="20dp"
                android:text="@string/share_title"
                android:textColor="@android:color/white" />

            <EditText
                android:id="@+id/share_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_above="@+id/share_submit"
                android:layout_marginLeft="20dp"
                android:layout_marginRight="20dp"
                android:background="@drawable/custom_textarea"
                android:gravity="top|left"
                android:lines="5"
                android:scrollbars="vertical"
                android:text="@string/default_msg" />

            <ImageView
                android:id="@+id/share_submit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_centerHorizontal="true"
                android:layout_margin="20dp"
                android:src="@drawable/photo_taken_btn_submit" />
        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

In the program , I set the params on the photo

    /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.share_pic_form);

            ctx = this;

            prefs = ctx.getSharedPreferences("userInfo", 0);
            editor = prefs.edit();

            tracker = EasyTracker.getInstance(this);

            permission = new ArrayList<String>();
            permission.add("email");

            Utility.setHeader(this,R.string.selfie_header,false);

            Session session = Session.getActiveSession();
            if (session == null) {
                if (savedInstanceState != null) {
                    session = Session.restoreSession(this, null, callback, savedInstanceState);
                }
                if (session == null) {
                    session = new Session(this);
                }
                Session.setActiveSession(session);
                if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                    session.openForRead(new Session.OpenRequest(this).setPermissions(permission).setCallback(callback));
                }
            }

            photoArea = (ImageView) findViewById(R.id.photo_area);
            int size = (int)(Utility.getScreenWidth(this)); 
            RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(size, size);
            imgParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
             //imgParams.addRule(RelativeLayout.ABOVE, R.id.share_title);
            photoArea.setAdjustViewBounds(true);
            photoArea.setLayoutParams(imgParams);
            photoArea.setBackgroundResource(android.R.color.darker_gray);
            photoArea.getBackground().setAlpha(204); // = 0.8 alpha

            shareTitle = (TextView) findViewById(R.id.share_title);
            shareContent = (EditText) findViewById(R.id.share_content);

            if (getIntent() != null) {
                Intent intent = getIntent();
                fileUri = (String) intent.getStringExtra("photo");
                catId = (String) intent.getStringExtra("catId");
            } else if (savedInstanceState != null){
                mBitmap = (Bitmap) savedInstanceState.getParcelable("bitmap") == null ? null : (Bitmap) savedInstanceState.getParcelable("bitmap");
                catId = (String) savedInstanceState.getString("catId");
            }

            FileInputStream inputStream = null;
            File imgSelected = new File(fileUri);

            try {
                inputStream = new FileInputStream(imgSelected);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(ctx, ctx.getResources().getString(R.string.get_photo_error), Toast.LENGTH_SHORT).show();
                finish();
            }

            mBitmap = Utility.decodeBitmap(inputStream, 1280, 960);

            if (mBitmap == null) {
                Toast.makeText(ctx, ctx.getResources().getString(R.string.get_photo_error), Toast.LENGTH_SHORT).show();
                finish();
            } else {
                photoArea.setImageBitmap(mBitmap);                  
                sharePhotoBtn = (ImageView) findViewById(R.id.share_submit);
                sharePhotoBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        tracker.send(MapBuilder.createEvent("form_button","Category_form","SubmitnShare_" + Utility.getLocale(ctx),null).build());
                        performPublish(PendingAction.POST_PHOTO);
                    }
                });
            }

        }


The problem is , in the xml /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.share_pic_form);

        ctx = this;

        prefs = ctx.getSharedPreferences("userInfo", 0);
        editor = prefs.edit();

        tracker = EasyTracker.getInstance(this);

        permission = new ArrayList<String>();
        permission.add("email");

        Utility.setHeader(this,R.string.selfie_header,false);

        Session session = Session.getActiveSession();
        if (session == null) {
            if (savedInstanceState != null) {
                session = Session.restoreSession(this, null, callback, savedInstanceState);
            }
            if (session == null) {
                session = new Session(this);
            }
            Session.setActiveSession(session);
            if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
                session.openForRead(new Session.OpenRequest(this).setPermissions(permission).setCallback(callback));
            }
        }

        photoArea = (ImageView) findViewById(R.id.photo_area);
        int size = (int)(Utility.getScreenWidth(this)); 
        RelativeLayout.LayoutParams imgParams = new RelativeLayout.LayoutParams(size, size);
        imgParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
         //imgParams.addRule(RelativeLayout.ABOVE, R.id.share_title);
        photoArea.setAdjustViewBounds(true);
        photoArea.setLayoutParams(imgParams);
        photoArea.setBackgroundResource(android.R.color.darker_gray);
        photoArea.getBackground().setAlpha(204); // = 0.8 alpha

        shareTitle = (TextView) findViewById(R.id.share_title);
        shareContent = (EditText) findViewById(R.id.share_content);

        if (getIntent() != null) {
            Intent intent = getIntent();
            fileUri = (String) intent.getStringExtra("photo");
            catId = (String) intent.getStringExtra("catId");
        } else if (savedInstanceState != null){
            mBitmap = (Bitmap) savedInstanceState.getParcelable("bitmap") == null ? null : (Bitmap) savedInstanceState.getParcelable("bitmap");
            catId = (String) savedInstanceState.getString("catId");
        }

        FileInputStream inputStream = null;
        File imgSelected = new File(fileUri);

        try {
            inputStream = new FileInputStream(imgSelected);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Toast.makeText(ctx, ctx.getResources().getString(R.string.get_photo_error), Toast.LENGTH_SHORT).show();
            finish();
        }

        mBitmap = Utility.decodeBitmap(inputStream, 1280, 960);

        if (mBitmap == null) {
            Toast.makeText(ctx, ctx.getResources().getString(R.string.get_photo_error), Toast.LENGTH_SHORT).show();
            finish();
        } else {
            photoArea.setImageBitmap(mBitmap);                  
            sharePhotoBtn = (ImageView) findViewById(R.id.share_submit);
            sharePhotoBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    tracker.send(MapBuilder.createEvent("form_button","Category_form","SubmitnShare_" + Utility.getLocale(ctx),null).build());
                    performPublish(PendingAction.POST_PHOTO);
                }
            });
        }

    }

The problem is , the layout_below photo_area for share_title is not working. It seems the photo_area is on top of it, how to fix the problem ? thanks

According to the docs layout_below :

Positions the top edge of this view below the given anchor view ID.

so, yes, the photo area in your case will be above

You, probably, would want to change to layout_above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM