2016-07-31 56 views
2

我有以下xml代码。 imagebutton没有在textView下面对齐,边距为5dp。我认为我在理解各种布局参数时犯了一个错误。相关布局是一个线性布局的子视图,其他视图很少。Layout_Below在相对布局中没有正确执行

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

     <com.github.lzyzsd.circleprogress.DonutProgress 
      android:id="@+id/amain_dp" 
      android:layout_width="240dp" 
      android:layout_height="240dp" 
      android:layout_centerInParent="true" /> 

     <TextView 
      android:id="@+id/amain_tv_currentsteps" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:textSize="25sp" /> 

     <ImageButton 
      android:id="@+id/amain_ib_whatsapp" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_marginTop="5dp" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/amain_tv_currentsteps" 
      android:background="@drawable/whatsapp_icon"/> 

    </RelativeLayout> 

这是我得到的输出:

enter image description here

编辑:代码工作如果适当layout_weight参数被删除,固定提供layout_height参数。不过想明白,为什么上面的代码将失败,因为layout_weight

编辑:完整的XML代码:

<ScrollView 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="in.jiyofit.basic_app.MainActivity"> 

<LinearLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/amain_tv_target" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="20dp" 
     android:layout_weight="10" /> 

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

     <com.github.lzyzsd.circleprogress.DonutProgress 
      android:id="@+id/amain_dp" 
      android:layout_width="240dp" 
      android:layout_height="240dp" 
      android:layout_centerInParent="true" /> 

     <TextView 
      android:id="@+id/amain_tv_currentsteps" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:textSize="25sp" /> 

     <ImageButton 
      android:id="@+id/amain_ib_whatsapp" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_marginTop="5dp" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/amain_tv_currentsteps" 
      android:background="@drawable/whatsapp_icon"/> 

    </RelativeLayout> 

    <FrameLayout 
     android:id="@+id/amain_fl_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="30" 
     android:layout_marginTop="20dp"/> 

</LinearLayout> 

+0

布局在我看来很好。您是否在Android Studio的布局编辑器中截取了截图? – Shaishav

+0

它来自我的手机。用'layout_weight'添加可能是原因的另一个视图。 – suku

+0

我的答案应该可以工作 –

回答

1

鉴于您的布局有以下方案:

<ScrollView> 
    <LinearLayout> 
     <TextView/> 
     <RelativeLayout/> 
     <FrameLayout/> 
    </LinearLayout> 
</ScrollView> 

在上面的层次结构,所有LinearLayout孩子有一个布局重属性集,基本上问父父之内提供(layout_weight/weightSum) * parentHeight()空间,它和LinearLayout本身有一个lauout_height设置为wrap_content,基本上要求孩子们说出自己的高度,所以LinearLayout本身可以相应地扩展。这是一种循环依赖,因此布局工具无法找出位置。

在这种情况下,你有两个选择:

1 - 删除ScrollViewLinearlayout's高度设置为match_parent。布局中的每个子元素都会相应地展开以正确占用屏幕空间。您的布局看起来就像这样:

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="80"> 

    <TextView 
     android:id="@+id/amain_tv_target" 
     android:layout_width="wrap_content" 
     android:layout_height="0dp" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="20dp" 
     android:layout_weight="10"/> 

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

     <com.github.lzyzsd.circleprogress.DonutProgress 
      android:id="@+id/amain_dp" 
      android:layout_width="240dp" 
      android:layout_height="240dp" 
      android:layout_centerInParent="true"/> 

     <TextView 
      android:id="@+id/amain_tv_currentsteps" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:gravity="center" 
      android:textSize="25sp"/> 

     <ImageButton 
      android:id="@+id/amain_ib_whatsapp" 
      android:layout_width="40dp" 
      android:layout_height="40dp" 
      android:layout_below="@id/amain_tv_currentsteps" 
      android:layout_centerHorizontal="true" 
      android:layout_marginTop="5dp"/> 

    </RelativeLayout> 

    <FrameLayout 
     android:id="@+id/amain_fl_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_marginTop="20dp" 
     android:layout_weight="30"/> 

</LinearLayout> 

2 - 既然你有一个适当的ScrollView,则可以提供绝对高度的LinearLayout每个孩子,让LinearLayout高度wrap_contentScrollView将针对不同屏幕充分缩放您的布局。你的布局可能会是这个样子:

<ScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical" 
     android:weightSum="80"> 

     <TextView 
      android:id="@+id/amain_tv_target" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="10dp" 
      android:layout_marginTop="20dp"/> 

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="400dp"> 

      <com.github.lzyzsd.circleprogress.DonutProgress 
       android:id="@+id/amain_dp" 
       android:layout_width="240dp" 
       android:layout_height="240dp" 
       android:layout_centerInParent="true"/> 

      <TextView 
       android:id="@+id/amain_tv_currentsteps" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_centerInParent="true" 
       android:gravity="center" 
       android:textSize="25sp"/> 

      <ImageButton 
       android:id="@+id/amain_ib_whatsapp" 
       android:layout_width="40dp" 
       android:layout_height="40dp" 
       android:layout_below="@id/amain_tv_currentsteps" 
       android:layout_centerHorizontal="true" 
       android:layout_marginTop="5dp"/> 

     </RelativeLayout> 

     <FrameLayout 
      android:id="@+id/amain_fl_container" 
      android:layout_width="match_parent" 
      android:layout_height="200dp" 
      android:layout_marginTop="20dp"/> 

    </LinearLayout> 

</ScrollView> 

就像一个经验法则,确保您LinearLayout的高度/宽度如果确定您提供layout_weight属性,其子之前。希望这可以帮助。

+0

我认为'linear_layout'必须是'wrap_content'内滚动视图。因此,如果使用'layout_weight',则不能使用第二种解决方案。 – suku

+0

@suku完全是你的电话 – Shaishav

0

您需要使用+ 这样的:

android:layout_below="@+id/amain_tv_currentsteps" 
+2

只有当您第一次使用视图的名称(无论是在创建时还是在引用过程中)时,才需要'+'。 – Shaishav

+0

对我来说,它做的工作 –

+0

什么工作?我说它不需要有'+'。它不会改变任何东西。 – Shaishav

0

在你顶部RelativeLayout给它一个尺寸0dp使问题,因为它(该rel有0高度和)你的图像视图低于我吨。

android:layout_height="match_parent" 
+0

它可能是一个'LinearLayout'的孩子。 – Shaishav

+0

@Charuka,我需要使用'layout_weight' – suku

+0

@Shaishav,是的,相对布局是一个子布线,它具有其他视图 – suku