Отображение imageView рядом с LinearLayout - PullRequest
0 голосов
/ 22 марта 2012

Я хочу отобразить картинку рядом с LinearLayout. Я хочу сделать следующее:

+---------------------------------------------------+
|                                                   |
|textView1       +------------+                     |
|button1         |imageView1  |                     |
|                |            |                     |
|                |            |                     |
|                |            |                     |
|                +------------+                     |
|                                                   |
|                                                   |
|                                                   |
+---------------------------------------------------+

Но с этим кодом:

<?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="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="96dp"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />

    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/picture" />

</LinearLayout>

Я, наверное, что-то подобное (я не вижу изображение):

+---------------------------------------------------+
|                                                   |
|textView1                                          |
|button1                                            |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
|                                                   |
+---------------------------------------------------+
 imageView1

Как я могу решить это? Заранее спасибо.

Ответы [ 4 ]

2 голосов
/ 22 марта 2012

Вам нужно использовать RelativeLayout для управления этим.

<?xml version="1.0" encoding="utf-8"?>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />


<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/picture" />

Теперь все на верхнем экране.Попробуйте настроить все, как вам нравится.

1 голос
/ 22 марта 2012

LinearLayout размещать представления в вертикальном или горизонтальном порядке. Для этого вам нужно использовать RelativeLayout .

Установите android:layout_centerInParent="true" на ImageView при использовании RelativeLayout , и он сделает волшебство

0 голосов
/ 22 марта 2012

Вам нужно включить свой ImageView в макет, чтобы достичь желаемого.

Попробуйте вместо этого этот 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="vertical" >

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="96dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_gravity = "center"
        android:layout_marginBottom = "40dp"
        android:layout_height="wrap_content"
        android:src="@drawable/picture" />

</LinearLayout>

</LinearLayout>

Итак, здесь вы сообщаете Android, что хотитеImageView, чтобы быть частью LinearLayout, конечно, вы можете добиться большего успеха, если используете RelativeLayout, но это решение вашей проблемы.

РЕДАКТИРОВАТЬ:

Если выхотите использовать Relative Layout для достижения того же самого, вот способ ..

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

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_marginTop="40dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Button" />
    </LinearLayout>

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>
0 голосов
/ 22 марта 2012

Используйте следующий код

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="96dp"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

</LinearLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/picture" 
    android:totheRightof=android:id="@+id/linearLayout1"
    android:marginLeft="20dp"/>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...