【问题标题】:How to put LinearLayout at the bottom of the screen and doesn't overlap with another LinearLayout?如何将LinearLayout放在屏幕底部并且不与另一个LinearLayout重叠?
【发布时间】:2015-02-11 08:07:48
【问题描述】:

我有 ScrollView,里面有 RelativeLayout,在 RelativeLayout 里面有 2 个 LinearLayout。它看起来像这样:

ScrollView
     RelativeLayout
          LinearLayout1
          LinearLayout2 

我想让我的 LinearLayout2 位于屏幕底部,并且它不应该与 LinearLayout1 重叠。当我包含android:layout_alignParentBottom="true" 行时,它显示它和底部,但是当TextView 较长时,LinearLayout2 与LinearLayout1 重叠。当我还包含android:layout_below="@+id/linearLayout" 行时,它会将LinearLayout2 放在LinearLayout1 下方,但不会将其放在底部。这是我的代码:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
    android:background="@drawable/background">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:background="#4D000000"
        android:layout_height="fill_parent">

        <LinearLayout xmlns:tools="http://schemas.android.com/tools"
            xmlns:custom="http://schemas.android.com/apk/res-auto"
            android:layout_width="fill_parent"
            android:id="@+id/linearLayout"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="?attr/actionBarSize"
            android:padding="5dp"
            tools:context="com.daimajia.slider.demo.MainActivity">

            <com.daimajia.slider.library.SliderLayout
                android:id="@+id/slider"
                android:layout_width="match_parent"
                custom:pager_animation="Default"
                custom:indicator_visibility="visible"
                custom:pager_animation_span="1100"
                android:layout_height="200dp" />

            <com.daimajia.slider.library.Indicators.PagerIndicator
                android:id="@+id/custom_indicator"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                custom:selected_color="#0095BF"
                custom:unselected_color="#55333333"
                custom:shape="oval"
                custom:selected_padding_left="5dp"
                custom:selected_padding_right="5dp"
                custom:unselected_padding_left="5dp"
                custom:unselected_padding_right="5dp"
                custom:selected_width="6dp"
                custom:selected_height="6dp"
                custom:unselected_width="6dp"
                custom:unselected_height="6dp" />

            <com.daimajia.slider.library.Indicators.PagerIndicator
                android:id="@+id/custom_indicator2"
                style="@style/AndroidImageSlider_Corner_Oval_Orange"
                android:layout_marginBottom="10dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

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

                <TextView
                    android:id="@+id/address"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Konstitucijos pr. 7A, Vilnius"
                    android:textColor="#FFFFFF" />

                <ImageButton
                    android:id="@+id/pin"
                    android:layout_width="32dp"
                    android:layout_height="32dp"
                    android:layout_alignParentRight="true"
                    android:layout_marginRight="5dp"
                    android:background="@drawable/pin" />
            </RelativeLayout>

            <View
                android:id="@+id/divider1"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#433b39"
                android:layout_marginTop="10dp" />

            <TextView
                android:id="@+id/work_hours"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="I - VI 10:00 - 21:00, VII 10:00 - 20:00"
                android:textColor="#FFFFFF"
                android:layout_marginTop="10dp" />

            <View
                android:id="@+id/divider2"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#433b39"
                android:layout_marginTop="10dp" />

            <TextView
                android:id="@+id/description"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:textColor="#FFFFFF" />

            <FrameLayout
                android:id="@+id/frame_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <fragment
                    android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
                    android:id="@+id/youtube_fragment"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
            </FrameLayout>

            <View
                android:id="@+id/divider3"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#575350"
                android:layout_marginTop="10dp" />

            <RelativeLayout
                android:layout_width="fill_parent"
                android:id="@+id/rl"
                android:layout_height="wrap_content">

                <RatingBar
                    android:id="@+id/ratingBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:numStars="5"
                    android:stepSize="1.0"
                    android:rating="0" />

                <TextView
                    android:layout_width="fill_parent"
                    android:id="@+id/rating_text"
                    android:layout_below="@+id/ratingBar"
                    android:layout_height="wrap_content"
                    android:gravity="center" />

            </RelativeLayout>

            <View
                android:id="@+id/divider4"
                android:layout_width="fill_parent"
                android:layout_height="1dp"
                android:background="#575350" />
        </LinearLayout>

        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_below="@+id/linearLayout"
            android:orientation="horizontal"
            android:weightSum="1.0">

            <ImageView
                android:id="@+id/number"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:layout_width="0dip"
                android:src="@drawable/telefonas" />

            <ImageView
                android:id="@+id/email"
                android:layout_height="wrap_content"
                android:layout_weight=".5"
                android:layout_width="0dip"
                android:src="@drawable/laiskas" />
        </LinearLayout>
    </RelativeLayout>
</ScrollView>

我做错了什么?

【问题讨论】:

  • 为什么不将相对布局切换为垂直方向的线性布局。孩子们将垂直放置而不会重叠

标签: android android-layout android-linearlayout android-relativelayout


【解决方案1】:

您目前正在将android:layout_below 分配给底部布局,删除它并将android:layout_above 分配给上面的布局。

由于您在 ScrollView 内,最好这样做:

保持android:layout_below 不变,但删除android:layout_alignParentBottom="true"

【讨论】:

  • 但是当 TextView 不是那么大(或者屏幕很大时,比如平板电脑)时,我的 LinearLayout 不会出现在屏幕底部
  • @linkas 嗯,你的问题有点复杂,因为当上部布局中有大量数据并且你需要滚动,我认为您必须以编程方式处理它。
【解决方案2】:

我注意到两件事。第一个是您使用 fill_parent 而不是 match_parent 。如果我没记错的话,这两者是等价的,但第一个已被弃用。
第二个是你没有在第一个 LinearLayout 中指定 android:layout_alignParentTop。尝试改变这两个东西,看看它们是否能解决你的问题

【讨论】:

  • Sarthak Mittal 的回答应该有效。不过,你应该记住我上面的建议
【解决方案3】:

我在@PedroHawk 的帮助下解决了这个问题。我只需要将这些布局的层次结构更改为:

ScrollView
     LinearLayout1
          RelativeLayout
              LinearLayout2 

【讨论】:

    猜你喜欢
    • 2013-01-24
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多