【问题标题】:Postioning of a relative layout at Bottom of parent Linear layout将相对布局定位在父线性布局的底部
【发布时间】:2014-06-05 14:26:19
【问题描述】:

我有一个包含子项的相对布局(3 个按钮和编辑文本)。最初布局放置在父布局的底部,这是一个线性布局。单击其中一个按钮时,我将其放置在屏幕顶部(父线性布局),我能够实现这一点。但是单击另一个按钮时,我希望它再次处于原始位置,即在屏幕底部。请提供任何帮助。这是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>

点击reply_button我将相对布局“post_layout”定位在屏幕顶部,我可以做到这一点,但是点击post_cancel_button我希望相对布局“post_layout”再次定位在屏幕底部。这是我用来将布局推送到屏幕顶部的代码:

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

按钮代码:

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 是我要定位的相对布局的名称,来自 xml 的 post_layout

【问题讨论】:

  • 你能发布你的代码吗?
  • @sripathi 我发布了代码以及 xml
  • 这个你试过了吗,rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
  • @sripathi 我已经在取消按钮上试过了 RelativeLayout.LayoutParams rparams = (RelativeLayout.LayoutParams) mPostLayout.getLayoutParams(); rparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,RelativeLayout.TRUE); mPostLayout.setLayoutParams(rparams);但是当我再次按下回复按钮时,它仍然在屏幕顶部而不是在底部
  • 你能添加所有按钮的clicklistener(在relativelayout中)吗?我怀疑您也可能会更改回复按钮中的布局参数。请确认..

标签: android android-layout android-relativelayout


【解决方案1】:

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

【讨论】:

  • 最初我设置了 android:layout_alignParentBottom="true"。我怎样才能得到原来的位置?对此我将不胜感激
  • 发布第一次点击和第二次点击的代码
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-05
  • 2014-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多