【问题标题】:How to align the layouts properly in Android?如何在 Android 中正确对齐布局?
【发布时间】:2016-08-08 03:15:31
【问题描述】:

我想将可滚动的TextViewRelativeLayout 上方对齐,底部有3 个按钮,此按钮上方有一个SeekBar。我的代码的问题是RelativeLayout 占据了整个屏幕。

这是我的代码:

    <?xml version="1.0" encoding="utf-8"?>
    <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:background="@drawable/images"
    android:orientation="vertical"
    tools:context="com.example.acer.aartisangrah.ekdanta">

    <ScrollView
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center">
    <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView9"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:enabled="true"
    android:focusable="true"
    android:longClickable="true"
    android:textIsSelectable="true" />
    </ScrollView>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<Button
    android:id="@+id/b9"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginLeft="10dp"
    android:onClick="decrease"
    android:background="@drawable/zoomout1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true" />

    <Button
    android:id="@+id/b10"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:onClick="increase"
    android:layout_marginRight="10dp"
    android:background="@drawable/zoomin1"/>

    <Button
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/button19"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:background="@drawable/play"/>

    <SeekBar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/seekBar"
    android:layout_above="@+id/b9"
    android:configChanges="orientation|screenSize"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_toStartOf="@+id/b10"
    android:layout_alignRight="@+id/b10"
    android:layout_alignEnd="@+id/b10"
    android:layout_weight="1"/>
    </RelativeLayout>
    </LinearLayout>

查看this image

【问题讨论】:

    标签: android android-layout alignment


    【解决方案1】:

    将ScrollView的高度改为android:layout_height="wrap_content" 并去掉android:layout_weight="1"

    【讨论】:

      【解决方案2】:

      以下布局应该可以完成这项工作。首先,&lt;RelativeLayout&gt;&lt;SeekBar&gt;之间的高度存在循环依赖关系,所以我固定了&lt;SeekBar&gt;的高度。其次,android:layout_alightParentBottom="true" 弄乱了布局管理器,因此将其删除。最后,将&lt;SeekBar&gt; 移到按钮上方,方便管理布局描述。这是更新的代码:

      <?xml version="1.0" encoding="utf-8"?>
      <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:background="@drawable/images"
                    android:orientation="vertical"
                    tools:context="com.example.acer.aartisangrah.ekdanta">
      
          <ScrollView
              android:layout_width="match_parent"
              android:layout_height="0dp"
              android:layout_weight="1"
              android:gravity="center">
              <TextView
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:gravity="center"
                  android:textAppearance="?android:attr/textAppearanceLarge"
                  android:id="@+id/textView9"
                  android:enabled="true"
                  android:focusable="true"
                  android:longClickable="true"
                  android:textIsSelectable="true" />
          </ScrollView>
      
          <RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content">
      
              <SeekBar
                  android:layout_width="match_parent"
                  android:layout_height="50dp"
                  android:id="@+id/seekBar"
                  android:configChanges="orientation|screenSize"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true"
                  android:layout_toStartOf="@+id/b10"
                  android:layout_alignRight="@+id/b10"
                  android:layout_alignEnd="@+id/b10"/>
      
              <Button
                  android:id="@+id/b9"
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_marginLeft="10dp"
                  android:onClick="decrease"
                  android:layout_below="@id/seekBar"
                  android:background="@drawable/zoomout1"
                  android:layout_alignParentLeft="true"
                  android:layout_alignParentStart="true" />
      
              <Button
                  android:id="@+id/b10"
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:layout_alignParentRight="true"
                  android:onClick="increase"
                  android:layout_below="@id/seekBar"
                  android:layout_marginRight="10dp"
                  android:background="@drawable/zoomin1"/>
      
              <Button
                  android:layout_width="50dp"
                  android:layout_height="50dp"
                  android:id="@+id/button19"
                  android:layout_below="@id/seekBar"
                  android:layout_centerHorizontal="true"
                  android:background="@drawable/play"/>
          </RelativeLayout>
      </LinearLayout>
      

      【讨论】:

      • 搜索栏指针与线正确对齐....指针稍微向上到线
      • @Devk 如果您说的是&lt;SeekBar&gt;,那么您需要从中删除android:layout_alignParentLeft="true"android:layout_alignParentStart="true"。这应该在我上面的布局中做到这一点:
      • 还是同样的问题
      • seekkar 必须从左下角的按钮开始。它对我来说很好。我不明白是什么问题。
      【解决方案3】:

      你的问题是你设置了ScrollViewandroid:layout_height="0dp"android:layout_weight="1"

      <ScrollView
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
      android:gravity="center">
      

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

      所以ScrollView 不为所动

      编辑

          <RelativeLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          android:layout_weight="1">
      

      您可以根据需要在 ScollView 和 RelativeLayout 之间设置android:layout_weight="1"。野兔都将获得一半(1/2)的屏幕。

      【讨论】:

      • 我不希望它是屏幕的 1/2 ......相对布局中包含的按钮和搜索栏应该在底部......我希望它像 whatsapp 一样
      • 不,我的意思是 scrollView 将占一半,而相对布局将占屏幕的其余一半...
      • 我试试你的代码……100% 没问题。只是在编辑之后尝试我..
      猜你喜欢
      • 1970-01-01
      • 2011-09-16
      • 2014-08-08
      • 1970-01-01
      • 2019-06-03
      • 1970-01-01
      • 2017-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多