1

I'm having a very difficult time figuring out how to lay out this element. Here's what I want to do (apologies for my MS Paint skills):

Layout

Here, the gray box is supposed to be an ImageView. The red lines here are just to make the sections more clear - not worried abot how to draw them. The features that I'm trying to get are:

  1. The ImageView is aligned relative to the topmost red line, and to the left edge of the green box.

  2. The "TEXT" is to the right of the image, and centered vertically in its section.

Without the ImageView, this is pretty straightforward - just a LinearLayout with three TextViews and some padding. I've tried doing this wit a RelativeLayout, but I'm unsure of how to center Text within the box without relying on guessing at the padding/margin.

I figured out how to get the box hanging over. I created my RelativeLayout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="20dp"
    android:background="#3AF">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:background="#AAA">
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:padding="10dp"
            android:id="@+id/textView"/>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:padding="10dp"
            android:id="@+id/textView1"/>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:padding="10dp"
            android:id="@+id/textView2"/>
    </LinearLayout>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher"
        android:id="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>
</RelativeLayout>

But what I can't figure out is how to align the topmost TextView to the right of the ImageView. (Note - this is just my attempt. The layout type/style is entirely flexible at this point).

Here's a screenshot of how it looks in the designer:

Screenshot

Any ideas?

4

2 回答 2

0
android:layout_toRightOf

应该工作......如果没有,你能解释什么是错的,你的问题是什么,因为它在这里工作正常......

于 2013-01-27T23:54:27.110 回答
0

尝试将您的第一行放入您的 LinearLayout,然后将其他所有内容放入您的 RelativeLayout。我没有尝试过这段代码,但如果我正确理解了你的场景,它应该可以解决问题。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_marginTop="20dp"
    android:background="#3AF">
    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_parent"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp"
        android:background="#AAA">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:src="@drawable/ic_launcher"
        android:id="@+id/imageView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"/>
     <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:padding="10dp"
            android:id="@+id/textView"/>
    </LinearLayout>

    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:padding="10dp"
            android:id="@+id/textView1"/>
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="New Text"
            android:padding="10dp"
            android:id="@+id/textView2"/>
    </LinearLayout>

</RelativeLayout>
于 2013-01-28T00:52:07.977 回答