Расположение изображения внутри ImageView с гравитацией: вверху - PullRequest
35 голосов
/ 28 апреля 2011
      ╔════════════════════════════════════════════════════════╗
      ║               Linear Layout  (Center)                  ║
      ║    ╔══════════════════════════════════════════════╗    ║   
      ║    ║ ImageView    ╔══════════════╗                ║    ║
      ║    ║              ║              ║    (Center)    ║    ║
      ║    ║              ║ Actual image ║                ║    ║
      ║    ║              ║              ║                ║    ║
      ║    ║              ║  (Center)    ║                ║    ║
      ║    ║              ║              ║                ║    ║
      ║    ║              ╚══════════════╝                ║    ║
      ║    ╚══════════════════════════════════════════════╝    ║ 
      ║                                                        ║
      ╚════════════════════════════════════════════════════════╝

И я хочу сделать это:

      ╔════════════════════════════════════════════════════════╗
      ║               Linear Layout  (Center)                  ║
      ║    ╔══════════════════════════════════════════════╗    ║   
      ║    ║ ImageView    ║              ║                ║    ║
      ║    ║              ║              ║    (Center)    ║    ║
      ║    ║              ║ Actual image ║                ║    ║
      ║    ║              ║              ║                ║    ║
      ║    ║              ║  (Top)       ║                ║    ║
      ║    ║              ║              ║                ║    ║
      ║    ║              ╚══════════════╝                ║    ║
      ║    ╚══════════════════════════════════════════════╝    ║ 
      ║                                                        ║
      ╚════════════════════════════════════════════════════════╝

Обратите внимание: изображение имеет центр тяжести с линейной компоновкой. Я хочу, чтобы изображение в окне просмотра имело гравитацию сверху, я пытаюсь установить гравитацию: сверху, но ничего не меняется Помоги мне

Редактировать: это мой xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:background="@color/all_white">
    <LinearLayout android:id="@+id/title_bar" style="@style/TitleBar">
        <TextView android:layout_width="wrap_content" 
            android:id="@+id/tv_printweb"
            android:singleLine="true" 
            android:ellipsize="end" 
            style="@style/ScreenTitleText" />
    </LinearLayout>
    <RelativeLayout android:orientation="vertical"
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent"
        android:layout_below="@+id/tv_web_filename" 
        android:layout_above="@+id/tv_web_page_num"
        android:layout_weight="1" 
        android:background="@color/background">
        <LinearLayout android:id="@+id/ln_web_pre"
            android:layout_width="30dp" 
            android:layout_height="fill_parent"
            android:layout_alignParentLeft="true" 
            android:gravity="center">
            <Button android:id="@+id/btn_web_pre" 
                android:layout_width="40dp"
                android:layout_height="120dp" 
                android:background="@drawable/bt_left"
                />
        </LinearLayout>
        <LinearLayout android:id="@+id/ln_web_zoomview"
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent"
            android:background="@color/all_white" 
            android:orientation="vertical"
            android:gravity="center" 
            android:layout_centerInParent="true">
            <ImageView android:id="@+id/web_zoomview"
                android:layout_width="fill_parent" 
                android:layout_height="wrap_content"
                android:background="@color/all_white" 
                android:layout_gravity="top"
                />

        </LinearLayout>
        <LinearLayout android:id="@+id/llCircle"
                android:layout_height="fill_parent" 
                android:layout_width="fill_parent"
                android:layout_marginRight="40dp" 
                android:layout_marginLeft="40dp" 
                android:layout_marginBottom="25dp" 
                android:layout_marginTop="25dp">
        </LinearLayout>
        <LinearLayout android:id="@+id/ln_web_next"
            android:layout_width="30dp" 
            android:layout_height="fill_parent"
            android:layout_alignParentRight="true" 
            android:gravity="center">
            <Button android:id="@+id/btn_web_next" 
                android:layout_width="40dp"
                android:layout_height="120dp" 
                android:background="@drawable/bt_right_1"
                />
        </LinearLayout>
        <RelativeLayout android:id="@+id/pagebar_web_top"
            android:layout_width="fill_parent" 
            android:layout_height="30dp"
            android:layout_alignParentTop="true" 
            android:gravity="center_vertical"
            android:paddingLeft="10dip" 
            android:paddingRight="10dip">
            <TextView android:id="@+id/tv_web_page_size"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true" 
                android:gravity="center"
                android:text="Paper Size" 
                android:textColor="@color/all_black"
                android:textStyle="bold" 
                android:background="@drawable/paper_border" />
        </RelativeLayout>

        <RelativeLayout android:id="@+id/web_pagebar"
            android:layout_width="fill_parent" 
            android:layout_height="50dp"
            android:layout_alignParentBottom="true" 
            android:gravity="left"
            android:paddingLeft="10dip" 
            android:paddingRight="10dip">
            <TextView android:id="@+id/tv_page_num"
                android:layout_width="wrap_content" 
                android:layout_height="wrap_content"
                android:layout_centerVertical="true" 
                android:gravity="center"
                android:text="1/1" 
                android:textColor="@color/all_black"
                android:textStyle="bold" 
                android:background="@drawable/paper_border" />
            <Button android:id="@+id/btn_web_orientation"
                android:layout_width="50dp" 
                android:layout_height="50dp"
                android:layout_alignParentRight="true" 
                android:background="@drawable/rotate_l" />
        </RelativeLayout>

    </RelativeLayout>
    <LinearLayout android:gravity="right|center_horizontal"
        android:id="@+id/toolbar" 
        style="@style/FunctionBar">
        <Button android:layout_width="wrap_content"
            android:layout_height="wrap_content" 
            android:id="@+id/btn_web_print"
            style="@style/FunctionButton" 
            android:text="@string/str_print" />
    </LinearLayout>
</LinearLayout>

Ответы [ 5 ]

88 голосов
/ 13 марта 2014

Установите scaleType в fitStart в вашем XML:

<ImageView
    android:id="@+id/ivMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitStart" >
</ImageView>
6 голосов
/ 28 апреля 2011

Пожалуйста, проверьте ответ Terel, который, как говорят, является правильным ответом.

В зависимости от того, что еще находится в макете, решением может быть использование другого макета (то есть FrameLayout, что довольно светло) вместо ImageView, и вместо реального изображения есть ImageView с android:layout_gravity="top".

5 голосов
/ 28 апреля 2011

Вы можете использовать ImageView.ScaleType.MATRIX и установить матрицу изображения, используя ImageView.setImageMatrix().Matrix можно получить, используя метод Matrix.setRectToRect().

3 голосов
/ 27 мая 2011

Вы пытались использовать атрибут android: gravity = "top" Я вижу, что для вашего ImageView вы используете Android: layout_gravity, возможно, вы могли бы попробовать использовать другой. Насколько я знаю, атрибут " android: layout_gravity " используется для установки гравитации элемента в его родительском элементе, а атрибут " android: gravity " для установки Гравитация содержимого элемента. (Кто-то поправит меня, если я ошибаюсь! = П хе-хе!)

2 голосов
/ 28 апреля 2011

Не будет ли проще использовать RelativeLayout? С android:layout_alignParentTop="true" и android:layout_centerHorizontal="true" вы можете достичь желаемого эффекта.

...