1

我想在屏幕中间显示一些文本,位于页眉和页脚下方。由于这段文字非常长,我将它嵌套在ScrollView中。我已经尝试了一些解决方案:thisthisthisthis,和一帮更多...Android,layout_above不能正常工作

起初我有两个relative_layouts重叠和文本被切的问题。最接受的答案是使用layout_abovelayout_below,但是当我使用layout_above时,文本从不显示

这是我的XML是什么样子:

<?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" 
android:orientation="vertical"> 

<RelativeLayout 
    android:id="@+id/linear_layout_header" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/gray" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:padding="5dp" 
    android:weightSum="3"> 

     <ImageView 
      android:id="@+id/profile_image" 
      android:layout_width="80dp" 
      android:layout_height="80dp" 
      android:layout_margin="10dp" 
      android:contentDescription="@string/profile_photo" 
      android:src="@drawable/default_profile" 
      tools:ignore="RtlHardcoded" /> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/scrollViewAndStuff" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentStart="true" 
    android:layout_above="@+id/ln_layout_footer" 
    android:layout_below="@+id/linear_layout_header"> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="10dp" 
     tools:ignore="UselessParent"> 

     <TextView 
      android:id="@+id/meetupDescription" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:text="@string/text"/>- 
    </ScrollView> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/ln_layout_footer" 
    android:layout_width="match_parent" 
    android:layout_alignParentBottom="true" 
    android:layout_height="match_parent" 
    android:gravity="bottom" 
    android:orientation="horizontal" 
    android:paddingTop="10dp"> 

    <LinearLayout 
     android:id="@+id/ln_layout" 
     android:layout_width="fill_parent" 
     android:background="@color/gray" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp"> 

     <TextView 
      android:id="@+id/red" 
      android:textColor="@color/white" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/red" /> 

     <TextView 
      android:id="@+id/blue" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:textColor="@color/white" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/blue" /> 

     <TextView 
      android:id="@+id/green" 
      android:layout_width="match_parent" 
      android:textColor="@color/white" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
     android:text="@string/green" /> 
    </LinearLayout> 

</RelativeLayout> 

+0

尝试将您的RelativeLayout与xml结尾处的文本(在您的页脚之后) – Rachit

+0

尝试仅使用android:layout_above =“@ id/ln_layout_footer”,+用于将新ID分配给查看 – Hmm

+0

@Rachit试过了,同样的问题... – Kemo

回答

3
ln_layout_footer

设置android:layout_height="wrap_content"


编辑

<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" 
android:orientation="vertical"> 

<RelativeLayout 
    android:id="@+id/linear_layout_header" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/dark_gray_pressed" 
    android:baselineAligned="false" 
    android:orientation="horizontal" 
    android:padding="5dp" 
    android:weightSum="3"> 

    <ImageView 
     android:id="@+id/profile_image" 
     android:layout_width="80dp" 
     android:layout_height="80dp" 
     android:layout_margin="10dp" 
     android:contentDescription="@string/action_settings" 
     android:src="@drawable/img_splash_logo" 
     tools:ignore="RtlHardcoded" /> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/scrollViewAndStuff" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/ln_layout_footer" 
    android:layout_below="@+id/linear_layout_header"> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="10dp" 
     tools:ignore="UselessParent"> 

     <TextView 
      android:id="@+id/meetupDescription" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      android:text="@string/txt_price" />- 
    </ScrollView> 

</RelativeLayout> 

<RelativeLayout 
    android:id="@+id/ln_layout_footer" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:gravity="bottom" 
    android:orientation="horizontal" 
    android:paddingTop="10dp"> 

    <LinearLayout 
     android:id="@+id/ln_layout" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="@color/dark_gray" 
     android:orientation="horizontal" 
     android:paddingBottom="5dp" 
     android:paddingTop="5dp"> 

     <TextView 
      android:id="@+id/red" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/txt_quantity" 
      android:textColor="@color/white" /> 

     <TextView 
      android:id="@+id/blue" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/txt_material" 
      android:textColor="@color/white" /> 

     <TextView 
      android:id="@+id/green" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_weight="1" 
      android:gravity="center" 
      android:text="@string/msg_enter_user_name" 
      android:textColor="@color/white" /> 
    </LinearLayout> 

</RelativeLayout> 

+0

我这样做了,我仍然有同样的问题,如果我添加layout_above我没有看到文本,如果我删除它的文本显示,但它是由页脚重叠。 – Kemo

+0

我创建你的布局它的工作完美........ –

+0

Chiraq是正确的。我重复了这种布局,一旦页脚被设置为wrap_content – Kuffs

1

在您使用布局两个属性就是为什么它不工作。所以,你可以删除一个属性这里是你的工作代码

<RelativeLayout 
    android:id="@+id/scrollViewAndStuff" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/linear_layout_header"> 

    <ScrollView 
     android:id="@+id/scrollView" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginLeft="20dp" 
     android:layout_marginRight="20dp" 
     android:layout_marginTop="10dp"> 

     <TextView 
      android:textColor="#000" 
      android:id="@+id/meetupDescription" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:layout_marginLeft="5dp" 
      android:text="@string/text" />- 
    </ScrollView> 

</RelativeLayout> 

您可以使用此android:layout_below="@+id/linear_layout_header或在此时间android:layout_above="@+id/ln_layout_footer,一个属性。或简单地复制并过去

+0

@Shive Kumar由Chirag.T提供的示例工作,他使用这两个属性。当我复制/粘贴他的代码时,它完美地工作,当我修改它时,我仍然遇到一些错误,但我想我可以修改他的代码。 – Kemo

相关问题