垂直布置按钮和文本墙

时间:2017-11-23 18:32:11

标签: android android-layout

我正在尝试垂直布局两个线性布局,第一个布局有时会有一个文本墙,有时候很短。在第二个线性布局中,还有两个垂直堆叠的按钮。

如果文本很短,如何使用按钮保留在屏幕底部的第二个线性布局?如果文本很长,如何保留在屏幕底部?

编辑:对不起,因为没有立即放置xml。就像这样

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical">

        <TextView
            android:id="@+id/text_pdtwo_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="2dp"
            android:lineSpacingExtra="5dp"
            android:text="@string/text_pd_two_intro_title"
            android:textColor="#000"
            android:textSize="23sp" />

        <ImageView
            android:id="@+id/img_pd_two_headshot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="16dp"
            android:src="@drawable/img_pd_two_sample" />

        <TextView
            android:id="@+id/text_pd_two_review_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="2dp"
            android:lineSpacingExtra="5dp"
            android:lineSpacingMultiplier="1.3"
            android:text="Lorem ipsum dolor sit amet.."
            android:textColor="#000"
            android:textSize="14sp" />


    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_marginBottom="16dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/button_pd_two_take_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:lineSpacingExtra="5dp"
            android:layout_marginBottom="16dp"
            android:text="@string/button_pd_two_take_photo"
            android:textColor="#000"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button_pd_two_select_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:lineSpacingExtra="5dp"
            android:text="@string/button_pd_two_select_gallery"
            android:textColor="#000"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button_pd_two_submit_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="16dp"
            android:lineSpacingExtra="5dp"
            android:text="@string/button_pd_two_submit_photo"
            android:textColor="#000"
            android:visibility="gone"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button_pd_two_cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:lineSpacingExtra="5dp"
            android:text="@string/button_pd_two_cancel"
            android:textColor="#fff"
            android:visibility="gone"
            android:textSize="15sp" />

    </LinearLayout>

</RelativeLayout>

当文字很短时,它看起来像这样,这很好

It looks like this

但是当文本很长时,就会出现这样的情况。我希望它在文本的末尾进行,并且可能在用户实际点击按钮之前将其向下滚动..

enter image description here

3 个答案:

答案 0 :(得分:2)

如果你不想在你的布局中添加ScrollView,你可以这样使用,通过这个没有人的视图(TextView或Button)将与其他视图重叠:

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:orientation="vertical"
        android:layout_above="@+id/bottom_layout">

        <TextView
            android:id="@+id/text_pdtwo_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="2dp"
            android:lineSpacingExtra="5dp"
            android:text="@string/text_pd_two_intro_title"
            android:textColor="#000"
            android:textSize="23sp" />

        <ImageView
            android:id="@+id/img_pd_two_headshot"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="16dp"
            android:src="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/text_pd_two_review_content"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginEnd="2dp"
            android:layout_marginStart="2dp"
            android:lineSpacingExtra="5dp"
            android:lineSpacingMultiplier="1.3"
            android:text="Lorem ipsum dolor sit amet.."
            android:textColor="#000"
            android:textSize="14sp" />


    </LinearLayout>

    <LinearLayout
        android:id="@+id/bottom_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:layout_marginBottom="16dp"
        android:orientation="vertical">

        <Button
            android:id="@+id/button_pd_two_take_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:lineSpacingExtra="5dp"
            android:layout_marginBottom="16dp"
            android:text="button_pd_two_take_photo"
            android:textColor="#000"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button_pd_two_select_gallery"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/colorPrimary"
            android:lineSpacingExtra="5dp"
            android:text="button_pd_two_select_gallery"
            android:textColor="#000"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button_pd_two_submit_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="16dp"
            android:lineSpacingExtra="5dp"
            android:text="button_pd_two_submit_photo"
            android:textColor="#000"
            android:visibility="gone"
            android:textSize="15sp" />
        <Button
            android:id="@+id/button_pd_two_cancel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:lineSpacingExtra="5dp"
            android:text="button_pd_two_cancel"
            android:textColor="#fff"
            android:visibility="gone"
            android:textSize="15sp" />

    </LinearLayout>

</RelativeLayout>

如果您的文字增加,我们需要通过此代码在文本中添加滚动:

/*For scrolling TextView*/
        text_pd_two_review_content.setMovementMethod(new ScrollingMovementMethod());
        text_pd_two_review_content.setOnTouchListener(new View.OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                // Disallow the touch request for parent scroll on touch of child view
                v.getParent().requestDisallowInterceptTouchEvent(true);
                return false;
            }
        });

答案 1 :(得分:0)

添加了layout_bove。 尝试使用thins方法:

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

<LinearLayout
    android:layout_above="@+id/button_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:orientation="vertical">

    <TextView
        android:id="@+id/text_pdtwo_title"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:layout_marginEnd="2dp"
        android:layout_marginStart="2dp"
        android:lineSpacingExtra="5dp"
        android:text="@string/text_pd_two_intro_title"
        android:textColor="#000"
        android:textSize="23sp" />

    <ImageView
        android:id="@+id/img_pd_two_headshot"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="16dp"
        android:src="@drawable/img_pd_two_sample" />

    <TextView
        android:id="@+id/text_pd_two_review_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginEnd="2dp"
        android:layout_marginStart="2dp"
        android:lineSpacingExtra="5dp"
        android:lineSpacingMultiplier="1.3"
        android:text="Lorem ipsum dolor sit amet.."
        android:textColor="#000"
        android:textSize="14sp" />


</LinearLayout>
<LinearLayout
    android:id="@+id/button_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_gravity="bottom"
    android:layout_marginBottom="16dp"
    android:orientation="vertical">

    <Button
        android:id="@+id/button_pd_two_take_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:lineSpacingExtra="5dp"
        android:layout_marginBottom="16dp"
        android:text="@string/button_pd_two_take_photo"
        android:textColor="#000"
        android:textSize="15sp" />
    <Button
        android:id="@+id/button_pd_two_select_gallery"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary"
        android:lineSpacingExtra="5dp"
        android:text="@string/button_pd_two_select_gallery"
        android:textColor="#000"
        android:textSize="15sp" />
    <Button
        android:id="@+id/button_pd_two_submit_photo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="16dp"
        android:lineSpacingExtra="5dp"
        android:text="@string/button_pd_two_submit_photo"
        android:textColor="#000"
        android:visibility="gone"
        android:textSize="15sp" />
    <Button
        android:id="@+id/button_pd_two_cancel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:lineSpacingExtra="5dp"
        android:text="@string/button_pd_two_cancel"
        android:textColor="#fff"
        android:visibility="gone"
        android:textSize="15sp" />

</LinearLayout>
</RelativeLayout>

答案 2 :(得分:0)

我能够使它工作,如果有更好的解决方案,请告诉我。

对于java文件

public class MainActivity extends AppCompatActivity {

Integer count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    final Integer vwheight = metrics.heightPixels;

    final LinearLayout top_layout = (LinearLayout) findViewById(R.id.top_layout);
    final LinearLayout bottom_layout = (LinearLayout) findViewById(R.id.bottom_layout);

    ViewTreeObserver vto = top_layout.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            if (count == 0) {
                int top_layoutMeasuredHeight = top_layout.getMeasuredHeight();

                if (top_layoutMeasuredHeight > vwheight) {

                    RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
                    relativeParams.addRule(RelativeLayout.BELOW, R.id.top_layout);

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                        relativeParams.removeRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
                    }

                    bottom_layout.setLayoutParams(relativeParams);
                    count++;
                }
            }
        }
    });

}
}

和XML

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:id="@+id/relative_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/top_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"

                android:layout_alignParentTop="true"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/text_pdtwo_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:layout_marginEnd="2dp"
                    android:layout_marginStart="2dp"
                    android:lineSpacingExtra="5dp"
                    android:text="@string/text_pd_two_intro_title"
                    android:textColor="#000"
                    android:textSize="23sp" />

                <ImageView
                    android:id="@+id/img_pd_two_headshot"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:layout_marginBottom="16dp"
                    android:src="@drawable/img_pd_two_sample" />

                <TextView
                    android:id="@+id/text_pd_two_review_content"
                    android:layout_width="match_parent"
                    android:layout_height="fill_parent"
                    android:layout_marginEnd="2dp"
                    android:layout_marginStart="2dp"
                    android:lineSpacingExtra="5dp"
                    android:lineSpacingMultiplier="1.3"
                    android:text="@string/text_pd_two_review_content"
                    android:textColor="#000"
                    android:textSize="14sp" />


            </LinearLayout>

            <LinearLayout
                android:id="@+id/bottom_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_marginBottom="@dimen/fab_margin"
                android:orientation="vertical">

                <Button
                    android:id="@+id/button_pd_two_take_photo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:background="@color/colorPrimary"
                    android:lineSpacingExtra="5dp"
                    android:text="button_pd_two_take_photo"
                    android:textColor="#fff"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button_pd_two_select_gallery"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/colorPrimary"
                    android:lineSpacingExtra="5dp"
                    android:text="button_pd_two_select_gallery"
                    android:textColor="#fff"
                    android:textSize="15sp" />

                <Button
                    android:id="@+id/button_pd_two_submit_photo"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="16dp"
                    android:lineSpacingExtra="5dp"
                    android:text="button_pd_two_submit_photo"
                    android:textColor="#000"
                    android:textSize="15sp"
                    android:visibility="gone" />

                <Button
                    android:id="@+id/button_pd_two_cancel"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:lineSpacingExtra="5dp"
                    android:text="button_pd_two_cancel"
                    android:textColor="#fff"
                    android:textSize="15sp"
                    android:visibility="gone" />

            </LinearLayout>

        </RelativeLayout>
    </ScrollView>

文字很长时的成品

enter image description here

如果有短文,则会保留在底部。

enter image description here