FrameLayout, LinearLayout, ReativeLayout怎么做到View在右下

实践出真知,不管什么问题,大家最好亲自动手映象才会更加深刻,就像是标题中的这个问题,相信大家都觉得这个问题十分简单,但是却很难表达出来,具体实现的布局文件代码如下:
ReativeLayout中的实现:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

        <TextView
            android:layout_alignParentBottom="true"
            android:layout_alignParentEnd="true"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2020offer"/>

</RelativeLayout>

FrameLayout中的实现:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

        <TextView
            android:layout_gravity="bottom|end"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2020offer"/>

</FrameLayout>

LinearLayout中的实现:
本文的重点就是在LinearLayout中做到右下显示,相信大家都遇到过这样一个问题:LinearLayout中控件不能居右对齐;而百度得到的大部分答案都是需要在LinearLayout布局中设置 android:layout_weight=“1” ,但为什么不能居右显示的道理其实很简单,安卓开发者文档中关于LinearLayout_orientation的说明中有这么一句:

“Should the layout be a column or a row? Use “horizontal” for a row, “vertical” for a column. The default is horizontal.”

意思是LinearLayout中orientation的值默认是horizontal,而当LinearLayout中的排列方式是horizontal时,只有垂直方向上的对齐方式才会生效,所以我们在LinearLayout布局中的控件的android:layout_gravity="right"设置不会让控件居右显示,具体解决方法如下:

  1. 设置LinearLayout布局宽度为match_parent;
    设置LinearLayout布局的gravity等于right;
    代码实现如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="right"
    tools:context=".TestActivity">

        <TextView
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2020offer"/>

</LinearLayout>
  1. 在控件外面再包一层LinearLayout
    设置内层LinearLayout布局宽度为wrap_content;
    此时设置内层LinearLayout布局的gravity等于right并不能使控件居右显示;
    需再在内层LinearLayout布局设置android:layout_weight=“1”(这样设置的目的其实也是让LinearLayout的宽度充满父控件,和直接设置LinearLayout布局宽度为match_parent是一样的);
    代码实现如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".TestActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right"
        android:layout_weight="1">

        <TextView
            android:layout_gravity="bottom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="2020offer"/>

    </LinearLayout>

</LinearLayout>

希望各位在日后的学习中也要多动手实践,本文如有什么纰漏,还望读者在评论中指出

发布了2 篇原创文章 · 获赞 4 · 访问量 2974

猜你喜欢

转载自blog.csdn.net/weixin_42719570/article/details/104276951