android - 垂直布局按钮和文本墙

标签 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

最佳答案

如果您不想在您可以通过这种方式使用的布局中添加 ScrollView,那么任何一个 View (TextView 或 Button)都不会与其他 View 重叠:

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

关于android - 垂直布局按钮和文本墙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47461618/

相关文章:

java - Android获取文本EditText到AlertDialog

android - 在 Android 应用程序中绘制思维导图的最佳方式是什么?

android - 以编程方式将进度条放在屏幕的中心

Android ListView 类似于 iOS UITableView

android - 在 firebase 上进行查询

java - 如何在 RxJava 中将两个对象合并到其中一个对象中?

android - 类重命名后 Gradle 测试运行失败

Android SqlCipher 性能问题

Android 启动画面计时器无法正常工作

android - AppCompat_v7 工具栏作为操作栏未显示菜单中的 'always' 操作,但 API 工具栏显示