2017-01-01 73 views
-1

我是Android开发新手,我无法理解为什么它不按照所需的输出工作。
下面显示了所需的输出和输出以及ml布局。RelativeLayout或LinearLayout?

Desired output

enter image description here

Achieved output

enter image description here

布局

<?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:id="@+id/activity_main" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 

tools:context="com.caringhumans.rds.caringhumans.MainActivity"> 
<ImageView 
    android:id="@+id/logo" 
    android:src="@drawable/logo" 
    android:layout_width="28dp" 
    android:layout_height="28dp" 
    android:layout_toLeftOf="@+id/caring"/> 

<TextView 
    android:id="@+id/caring" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Caring Humans" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:textSize="25sp" 
    android:textColor="#f70f59"/> 
<ImageView 
    android:id="@+id/child" 
    android:src="@drawable/child" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/caring"/> 
    <TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Campaign" 
    android:textColor="#f70f59" 
    android:textSize="22sp" 
    android:layout_below="@+id/child"/> 
    </RelativeLayout> 
+0

RelativeLayout,它将帮助您稍后支持多种屏幕尺寸 –

回答

0

LinearLayouts用于放置您的观点。线性SH猿,无论是在一排或一列。
后者似乎是你以后的样子。

当您想以非线性方式放置视图的位置时,使用RelativeLayouts,而不是使用它们之间的关系(toRightOf,above或similar)。

+0

我想过使用LinearLayout,但后来我无法理解如何将文本“Caring Humans”居中并将徽标放在它之前。 –

+0

要“将徽标放在它之前”,您可以使用**复合可绘制**(图像将在** TextView内**,并且您可以摆脱ImageView-less视图,更好的性能)。要将TextView的内容与中心对齐,您可以使用'android:gravity =“center”'。 –

0

为了使ImageView的真正“包装”的形象在你身边正在显示您需要添加这两个属性

android:scaleType="fitXY" 
android:adjustViewBounds="true" 

文档:https://developer.android.com/reference/android/widget/ImageView.ScaleType.html

如父布局在这里,你或许应该使用LinearLayouts(您将需要添加到它们android:orientation="vertical"作为默认的一个是水平的)

0

将您的代码更改为:

<?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:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    tools:context="com.caringhumans.rds.caringhumans.MainActivity"> 
<ImageView 
android:id="@+id/logo" 
android:src="@drawable/logo" 
android:layout_width="28dp" 
android:layout_height="28dp" 
android:layout_toLeftOf="@+id/caring"/> 

<TextView 
android:id="@+id/caring" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Caring Humans" 
android:layout_alignParentTop="true" 
android:layout_centerHorizontal="true" 
android:textSize="25sp" 
android:textColor="#f70f59"/> 
<ImageView 
android:id="@+id/child" 
android:src="@drawable/child" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_above="@+id/text" 
android:layout_below="@+id/caring"/> 
<TextView 
android:id="@+id/text" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="Campaign" 
android:textColor="#f70f59" 
android:textSize="22sp" 
android:layout_alignParentBottom="true"/> 
    </RelativeLayout> 
1

试试这个

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/activity_main" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/logo" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:drawableLeft="@drawable/logo" 
     android:gravity="center" 
     android:text="Caring Humans" 
     android:textColor="#f70f59" 
     android:textSize="25sp" /> 

    <ImageView 
     android:id="@+id/child" 
     android:layout_width="match_parent" 
     android:layout_height="200dp" 
     android:layout_below="@+id/logo" 
     android:adjustViewBounds="true" 
     android:scaleType="center" 
     android:src="@drawable/child" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/child" 
     android:paddingLeft="20dp" 
     android:paddingRight="20dp" 
     android:text="Campaign" 
     android:textColor="#f70f59" 
     android:textSize="22sp" /> 
</RelativeLayout> 

问题是与图像试图修复这一点,你的问题就可以解决。

+0

谢谢,你的代码工作得很好,只有改变我为解决图像而添加android:scaleType =“fitXY”。 –

+0

谢谢....请接受答案,如果这对你有用。 –

0

将以下两个标签添加到ID为'child'的ImageView可以解决您的问题。

android:scaleType="fitXY" 
android:adjustViewBounds="true" 

FitXY规模使用图像填充 ,你需要设置adjustViewBounds到真正调整它的界限,以保持其绘制的长宽比。

0
<ImageView 
android:id="@+id/child" 
android:src="@drawable/child" 
android:layout_width="wrap_content" 
android:scaleType="centerCrop" 
android:adjustViewBounds="true" 
android:layout_height="256dp" 
android:layout_below="@+id/caring"/> 

这将帮助您实现所需的布局。根据您的需要增加图像的高度。 :)