Android: панель с центрированным изображением и кнопками справа

1

Я действительно борюсь с частью макета в своем приложении. У меня есть панель заголовка с центрированным изображением, а справа должна быть две кнопки (или кликабельные изображения, как у меня сейчас). Мне удалось получить изображения рядом с eachother (все в центре), но я хочу, чтобы изображения справа. Когда я использую layout_weight, чтобы "первый" растянуть, кнопки справа, но они становятся такими маленькими. Я хочу, чтобы кнопки (изображения) сохраняли свои первоначальные размеры. Я даже пытался сделать hardcode, что с minWidth и minHeight, но это не работает.

Вот код, который центрирует все:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/header_tile">

    <ImageView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:src="@drawable/header_logo">
    </ImageView>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

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

        <ImageView
           android:id="@+id/next"
           android:src="@drawable/omlaag"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        </ImageView>
    </LinearLayout>
</LinearLayout>

И вот код, который работает, за исключением того, что последнее изображение становится настолько маленьким:

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="@drawable/header_tile">

    <ImageView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:src="@drawable/header_logo"
        android:layout_weight="2">
    </ImageView>

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

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

        <ImageView
           android:id="@+id/next"
           android:src="@drawable/omlaag"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        </ImageView>
    </LinearLayout>
</LinearLayout>

Любые идеи? Большое спасибо!

Эрик

Теги:
layout

3 ответа

1
Лучший ответ

Используйте относительный макет, он гораздо более мощный, обратите внимание на атрибуты layout_centerInParent и layout_alignParentRight:

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/header_tile">
    <ImageView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:src="@drawable/header_logo"
        android:layout_centerInParent="true">
    </ImageView>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true">
        <ImageView
           android:id="@+id/previous"
           android:src="@drawable/omhoog"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        </ImageView>
        <ImageView
           android:id="@+id/next"
           android:src="@drawable/omlaag"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content">
        </ImageView>
    </LinearLayout>
</RelativeLayout>
  • 0
    Прекрасно работает, не знал о RelativeLayout до сих пор. Отлично!
0

  XMLNS: андроид = "http://schemas.android.com/apk/res/android" >

<LinearLayout android:id="@+id/LinearLayout01"
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:layout_weight="1" 
            android:gravity="center">
     <Button android:text="@+id/Button03"
         android:id="@+id/Button03" 
         android:layout_height="wrap_content"
          android:layout_weight="0" 
          android:layout_width="wrap_content">
    </Button>
</LinearLayout>

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

Не знаю размер ваших изображений, поэтому я использовал кнопки. Может ли значение веса равняться 2? 0 или 1, я всегда использую.

0

layout_weight работает по-разному при использовании с fill_parent и wrap_content;

  • wrap_content: он пытается развернуть элементы в соотношении весов, чтобы покрыть все пространство

  • fill_parent: он пытается сжать элементы в отношении веса.

Все представления должны иметь один и тот же тип (fill_parent/wrap_content) при использовании весов

Ещё вопросы

Сообщество Overcoder
Наверх
Меню