请检查以下图片的链接。由于声誉低下,我无法发布图片。
http://i.stack.imgur.com/nupZo.png
我的目标是要获得一种类似于左图的布局,而不是获得一种如右图的布局。这是我的布局的xml代码。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@color/metalDark"
android:orientation="vertical"
>
<LinearLayout
android:id="@+id/topPanel"
android:layout_width="fill_parent"
android:layout_height="65dp"
android:background="@color/metalList"
android:elevation="6dp"
android:layout_marginBottom="5dp"
android:orientation="horizontal">
<TextView
.../>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/topPanel"
android:layout_margin="10dp"
android:orientation="vertical">
<LinearLayout
android:id="@+id/albumheader"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/metalList"
android:elevation="6dp">
<LinearLayout
android:id="@+id/underlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="25dp"
android:layout_marginBottom="25dp"
android:background="@drawable/canvas2">
<ImageView
android:id="@+id/al_art"
android:layout_width="135dp"
android:layout_height="135dp"
android:layout_toLeftOf="@+id/al_details"
android:layout_marginLeft="25dp"
android:layout_marginTop="-17dp"
android:elevation="4dp"/>
<LinearLayout
android:id="@+id/al_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="right|center"
android:orientation="vertical">
<TextView
.../>
<TextView
.../>
<TextView
.../>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="@color/metalList"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp"
android:elevation="6dp"
>
<ListView
..../>
</LinearLayout>
</LinearLayout>
LinearLayout
Views
彼此叠加并不是很好。
本文对于获取更多信息非常有帮助:http : //android-developers.blogspot.com/2009/02/android-layout-tricks-1.html
这是我会做的:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_marginBottom="12dp"
android:layout_height="?listPreferredItemHeight"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img_orange_rectangle"
android:background="#ff6600"
android:layout_centerVertical="true"
android:layout_width="match_parent"
android:layout_height="128dp"/>
<TextView
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:text="hi I'm some text"
android:gravity="end|top"
android:padding="6dp"
android:layout_alignStart="@id/img_orange_rectangle"
android:layout_alignEnd="@id/img_orange_rectangle"
android:layout_alignBottom="@id/img_orange_rectangle"
android:layout_alignTop="@id/img_orange_rectangle"
android:layout_height="0dp"/>
<ImageView
android:id="@+id/img_blue_square"
android:background="#2541ba"
android:layout_marginStart="24dp"
android:layout_centerVertical="true"
android:layout_width="156dp"
android:layout_height="156dp"/>
</RelativeLayout>
<ListView
android:id="@+id/listview"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:layout_marginBottom="12dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
根据以下评论,于2015年8月15日更新:
但是,请告诉我为什么将RelativeLayout的layout_height保持为0?
我在上使用了0的高度,RelativeLayout
因为布局基于来获取其高度layout_weight
。
橙色矩形的高度会根据设备和屏幕尺寸而变化吗?
不会。尽管我已经更新了布局以能够这样做。我推荐这篇文章,以了解如何支持多种屏幕尺寸。在此StackOverflow第2部分的底部,它讨论了Google IO pdf中每个屏幕尺寸的xml尺寸。
另外,如果我想将文本放入橙色矩形内,如何确保将其保留在橙色矩形内?
我在上面添加了另一种布局。有多种解决方法。我选择的是使它TextView
与ImageView
上面的属性对齐。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句