Android相对布局展示位置

时间:2014-06-17 04:48:29

标签: android xml android-layout

我试图在Android上创建一个包含常规主页图标列表的应用程序。 问题是,我无法很好地管理它。 这是我已经完成的图标的图片。

https://scontent-a-sin.xx.fbcdn.net/hphotos-xfa1/t1.0-9/10487441_10203329703355576_1539494398961024590_n.jpg

这是我创建的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:orientation="horizontal" >


<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="5"
        android:gravity="center"
        android:padding="5dip" >

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <ImageView
                android:id="@+id/iconFoods"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentTop="true"
                android:layout_margin="0dp"
                android:contentDescription="@string/desc"
                android:layout_centerInParent="true"                
                android:src="@drawable/food"
                android:clickable="true"
                android:onClick="onClick" />

            <TextView
                android:id="@+id/txtCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignRight="@+id/iconFoods"
                android:layout_margin="0dp"
                android:background="@drawable/shapecount"
                android:paddingLeft="3dp"
                android:paddingRight="3dp"
                android:text="50"
                android:textColor="#FFFFFF"
                android:textSize="12sp"
                android:textStyle="bold" />          

            <TextView
                android:id="@+id/textView1"
                android:textSize="12sp"
                android:layout_marginTop="20dip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:layout_alignBottom="@+id/iconFoods"
                android:layout_alignParentLeft="false"
                android:text="Medium Text" />
        </RelativeLayout>

我的问题是,如何将图像放在相对布局的中心以及如何设置图像和文本视图之间的空间。 我已经尝试将填充顶部和布局边距放在顶部,但它仍然无效。

4 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

  1. 使用android:layout_below代替android:layout_alignBottom。因此,保证金不起作用。

  2. 删除 android:layout_alignParentLeft =“true”和 android:layout_alignParentTop =“true”这些属性

  3. RelativeLayout's宽度match_parent设为正确 weight就可以了。

  4. P.S:嵌套权重不好

    试试这个

    <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >
    
            <ImageView
                android:id="@+id/iconFoods"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="0dp"
                android:contentDescription="@string/desc"
                android:layout_centerInParent="true"                
                android:src="@drawable/food"
                android:clickable="true"
                android:onClick="onClick" />
    
            <TextView
                android:id="@+id/txtCount"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_alignRight="@+id/iconFoods"
                android:layout_margin="0dp"
                android:background="@drawable/shapecount"
                android:paddingLeft="3dp"
                android:paddingRight="3dp"
                android:text="50"
                android:textColor="#FFFFFF"
                android:textSize="12sp"
                android:textStyle="bold" />          
    
            <TextView
                android:id="@+id/textView1"
                android:textSize="12sp"
                android:layout_marginTop="20dip"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:layout_centerHorizontal="true"
                android:layout_below="@+id/iconFoods"
                android:text="Medium Text" />
        </RelativeLayout>
    

答案 2 :(得分:0)

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

    <ImageView
        android:id="@+id/icon"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_centerHorizontal="true"
        android:background="@drawable/ic_launcher"
        android:scaleType="center" />


    <TextView
        android:id="@+id/tv1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="-10dip"
        android:layout_toRightOf="@+id/icon"
        android:gravity="center"
        android:textColor="#ff0000"
        android:text="@string/app_name" />


    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#000000"
        android:layout_below="@+id/icon"
        android:layout_centerHorizontal="true"
        android:text="Meduim Text" />

    </RelativeLayout>

这是输出

Image

使用 GridView ,将其作为自定义布局。它对你有用。

答案 3 :(得分:0)

您必须对视图属性进行一些更改 ...

<{1>}中的

而不是

textView1使用

<强> android:layout_alignBottom="@+id/iconFoods"

然后使用保证金属性 ...它会起作用...

使用以下代码...

android:layout_below="@+id/iconFoods"