【问题标题】:TextView or Layout stay at the bottom of a <fragment>TextView 或 Layout 停留在 <fragment> 的底部
【发布时间】:2016-09-30 16:04:01
【问题描述】:

如何让视图停留在片段的底部。在下面的代码中,我有一个带有地图片段的 LinearLayout,在片段内部有一个 EditText 位于片段顶部的中心。如何在

之后立即添加 Textview
<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:orientation="vertical"
tools:context=".MainActivity">

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_alignParentTop="true"
            android:drawableLeft="@drawable/ic_search"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:hint="Search"
            />



    </RelativeLayout>


</fragment>
//How can i make these Text view to the buttom
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:textColor="@color/bgprofil"
    android:layout_alignParentTop="true"
    android:gravity="bottom"
    />

【问题讨论】:

  • android:layout_alignParentTop="true" 是与RelativeLayout 一起使用的属性android:gravity 仅受TextView 内的文本影响如果要将TextView 放在EditText 下面,请将TextView 放在RelativeLayout 中

标签: java android xml android-layout


【解决方案1】:

你可以使用android:layout_weight="1",这样fragment view会占据你LinearLayout的可用空间,所以textView会留在底部

<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:orientation="vertical"
tools:context=".MainActivity">

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="0dp" 
     android:layout_weight="1"
    class="com.google.android.gms.maps.SupportMapFragment" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_horizontal"
        >

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/white"
            android:layout_alignParentTop="true"
            android:drawableLeft="@drawable/ic_search"
            android:layout_marginTop="@dimen/activity_horizontal_margin"
            android:layout_marginLeft="@dimen/activity_horizontal_margin"
            android:layout_marginRight="@dimen/activity_horizontal_margin"
            android:hint="Search"
            />



    </RelativeLayout>


</fragment>
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:textColor="@color/bgprofil"
     />
</LinearLayout>

【讨论】:

  • 试过了,但没有用。试图实现类似链接image中的图像
【解决方案2】:

如果您对视图组不严格,可以使用这种方法来实现您想要的设计。

<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">

    <fragment
        android:id="@+id/map"
        class="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_horizontal_margin"
        android:background="@color/white"
        android:gravity="center"
        android:drawableLeft="@drawable/ic_search"
        android:hint="Search for Address"
        android:paddingBottom="8dp"
        android:paddingTop="8dp" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@color/white"
        android:textColor="@color/bgprofil"
        android:padding="8dp" />
</RelativeLayout>

【讨论】:

    【解决方案3】:

    在Linearlayout中使用android:gravity属性你可以获得你想要的视图。

    在您的情况下,您希望您的文本视图位于屏幕的片段和底部下方, 在那种情况下:

    1 android:weightSum=1 在父线性布局中

    2 android:weight=0.9 在片段中

    3 android:weight=0.1 在文本视图中

    使用此方法,您可以获得任何屏幕尺寸的所需视图。

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2015-01-28
      • 1970-01-01
      • 2018-10-18
      • 2011-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-27
      相关资源
      最近更新 更多