在Android中的Layout中插入ImageView

时间:2015-03-11 12:40:10

标签: android android-layout

我使用LinearLayout和RelativeLayout组合创建了一个4矩形布局。现在我想在前两个布局的中心插入黄色图像(轮廓图像),如下图所示:

enter image description here

我无法这样做?这是我尝试过的。这样就可以在开始时正确地设置2个布局。

<LinearLayout 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:background="@android:color/darkgreen"
android:orientation="vertical">

<LinearLayout
    android:id="@+id/profilenameemailID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight=".12"
    android:orientation="horizontal"
    android:paddingLeft="2dp"
    android:paddingRight="2dp"
    android:paddingTop="2dp" >

    <RelativeLayout
        android:id="@+id/profilelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight=".50"
        android:background="@color/light_green"
        android:gravity="center" >

        <ImageView
            android:id="@+id/icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/namesysmbol"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/profilenameText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/icon"
            android:paddingLeft="2dp"
            android:text="@string/defaultname"
            android:textColor="@color/nametext"
            android:textSize="14sp" />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/emailID"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="1dp"
        android:layout_weight=".50"
        android:background="@color/light_green"
        android:gravity="center" >

        <ImageView
            android:id="@+id/eicon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/emailsymbol"
            android:paddingBottom="5dp"
            android:paddingTop="5dp"
            android:visibility="visible" />

        <TextView
            android:id="@+id/emailText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/sicon"
            android:paddingLeft="2dp"
            android:text="@string/defaultEmail"
            android:textColor="@color/emailtext"
            android:textSize="14sp" />
    </RelativeLayout>
</LinearLayout>
</LinearLayout>

有人可以帮我将黄色imageView插入中心,格式如上。

谢谢!

3 个答案:

答案 0 :(得分:2)

您可以尝试使用此Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

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

        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="#bb4"
                android:layout_weight="1"
                android:layout_margin="4dp">

            </LinearLayout>

            <LinearLayout
                android:layout_weight="1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:background="#bb4"
                android:layout_margin="4dp">

            </LinearLayout>
        </LinearLayout>

        <!-- Your ImageView here!-->
        <View
            android:layout_centerInParent="true"
            android:layout_width="90dp"
            android:layout_height="90dp"
            android:background="#ff5"/>

    </RelativeLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="170dp">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#bb4"
            android:layout_weight="1"
            android:layout_margin="4dp">

        </LinearLayout>

        <LinearLayout
            android:layout_weight="1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            android:background="#bb4"
            android:layout_margin="4dp">

        </LinearLayout>
    </LinearLayout>

</LinearLayout>

答案 1 :(得分:2)

这个伪代码可以帮助你理解概念

<LinearLayout orientation= vertical>
<FrameLayout>

<LinearLayout orientation= horizontal>
// make to rectangle here
</LinearLayout>

<Image/> create rounded image here with layout_gravity center

</FrameLayout>

<FrameLayout>

<LinearLayout orientation= horizontal>
// make to rectangle here
</LinearLayout>

</FrameLayout>

</LinearLayout>

答案 2 :(得分:0)

复制并粘贴到xml文件中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#E3E3E3" >

<RelativeLayout
    android:id="@+id/r_1"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp" >

    <View
        android:id="@+id/view1"
        android:layout_width="5dp"
        android:layout_height="1dp"
        android:layout_centerInParent="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/view1"
        android:background="#666" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/view1"
        android:background="#666" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:background="#ff00ff" />
</RelativeLayout>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:layout_below="@+id/r_1"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"
    android:layout_marginTop="5dp" >

    <View
        android:id="@+id/view2"
        android:layout_width="5dp"
        android:layout_height="1dp"
        android:layout_centerInParent="true" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toLeftOf="@+id/view2"
        android:background="#666" >
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_toRightOf="@+id/view2"
        android:background="#666" >
    </RelativeLayout>

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:background="#ff00ff" />
</RelativeLayout>