简体   繁体   中英

Postioning of a relative layout at Bottom of parent Linear layout

I have a relative layout having children(3 buttons and edittext). Initially the layout is placed at bottom of the parent layout which is a linear layout. On click of one of the buttons I position it on top of the screen(parent linear layout), I am able to achieve this. But on click of another button i want it to be at original position again ie at bottom of the screen. Any help please. Here is the xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/msg_detail_bg"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/TitleBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:orientation="horizontal" >

        <ImageButton
            android:id="@+id/back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:src="@drawable/backbutton" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp"
            android:singleLine="true"
            android:textSize="30sp" />
    </LinearLayout>



        <RelativeLayout
        android:id="@+id/post_layout"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:layout_alignParentBottom="true"
        android:visibility="gone" >


        <ImageView
            android:id="@+id/post_upd_frame1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/post_upd_frame" />

        <EditText
            android:id="@+id/update_edit_view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@id/post_upd_frame1"
            android:layout_alignRight="@id/post_upd_frame1"
            android:background="@android:color/transparent"
            android:inputType="textMultiLine"
            android:maxLines="5"
            android:singleLine="false"
            android:focusable="true"
            android:padding="10dp" 
            android:visibility="gone">
        </EditText>

        <ImageButton
            android:id="@+id/post_btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/post_upd_frame1"
            android:layout_alignRight="@id/post_upd_frame1"
            android:layout_margin="5dp"
            android:layout_marginLeft="5dp"
            android:background="@android:color/transparent"
            android:src="@drawable/post_message" />

        <ImageButton
            android:id="@+id/post_picture_btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/post_upd_frame1"
            android:layout_margin="5dp"
            android:layout_marginLeft="5dp"
            android:layout_toLeftOf="@id/post_btn1"
            android:background="@android:color/transparent"
            android:src="@drawable/add_picture" />

        <ImageButton
            android:id="@+id/post_Cancel_btn1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBottom="@id/post_upd_frame1"
            android:layout_alignParentLeft="true"
            android:layout_margin="5dp"
            android:layout_toLeftOf="@id/post_picture_btn1"
            android:background="@android:color/transparent"
            android:src="@drawable/cancel_message" />

        <ImageView
            android:id="@+id/post_update_spinner_view1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/post_btn1"
            android:layout_centerHorizontal="true"
            android:src="@drawable/running_indicator"
            android:visibility="gone" />



    </RelativeLayout>

     <RelativeLayout
            android:id="@+id/reply_action_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:layout_above="@id/post_layout1"
            android:orientation="horizontal">

        <ImageButton
            android:id="@+id/delete_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:background="@android:color/transparent"
            android:paddingBottom="5dp"
            android:src="@drawable/xml_delete_button_selector" />

        <ImageButton
            android:id="@+id/reply_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="10dp"
            android:layout_toRightOf="@id/delete_button"
            android:background="@android:color/transparent"
            android:paddingBottom="5dp"
            android:src="@drawable/xml_reply_button_selector" />

        <TextView
            android:id="@+id/time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignLeft="@+id/sent_received"
            android:layout_alignTop="@+id/reply_button"
            android:layout_alignParentRight="true"
            android:layout_marginRight="5dp"
            android:layout_margin="10dp"
            android:textColor="@android:color/white"
            android:textSize="14sp" />

        <ImageView
            android:id="@+id/sent_received"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"          
            android:layout_alignTop="@+id/reply_button"
            android:background="@android:color/transparent"
            android:src="@drawable/received" />

        </RelativeLayout>

    </RelativeLayout>

</LinearLayout>

On clicking reply_button I position the relative layout "post_layout" at top of screen, I am able to do this, but on clicking of post_cancel_button I want the relative layout "post_layout" to be positioned at bottom of screen again. here is the code which I am using to push layout to top of screen:

RelativeLayout.LayoutParams rparams = (RelativeLayout.LayoutParams) mPostLayout.getLayoutParams();
                                        rparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            mPostLayout.setLayoutParams(rparams);  

Code for buttons:

mReplyButton.setOnClickListener(new OnClickListener() {

        @Override
            public void onClick(View v) {

                   RelativeLayout.LayoutParams rparams = (RelativeLayout.LayoutParams) mPostLayout.getLayoutParams();
                                        rparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
            mPostLayout.setLayoutParams(rparams);  

            InputMethodManager imm = (InputMethodManager)getSystemService(

                    Context.INPUT_METHOD_SERVICE);

            imm.showSoftInput(mUpdateEditView, InputMethodManager.SHOW_FORCED);


            }
    });

    mMessageReplyCancelButton.setOnClickListener(new OnClickListener() {

        @Override
            public void onClick(View v) {

                RelativeLayout.LayoutParams rparams = (RelativeLayout.LayoutParams) mPostLayout.getLayoutParams();
    rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    mPostLayout.setLayoutParams(rparams);

            }
    });

mPostLayout is name of relative layout I want to position, post_layout from xml

当您第一次单击时,获得原始位置,第二次单击,然后重置为原始位置

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