使用重力将图像放置在 ImageView 内:top

发布于 2024-11-03 20:25:44 字数 7202 浏览 7 评论 0原文

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

我想这样做:

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

请注意:imageview具有线性布局的重心 我希望图像视图中的图像具有重力顶部,我尝试设置重力:顶部,但没有任何变化。帮我

编辑:这是我的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>
      ╔════════════════════════════════════════════════════════╗
      ║               Linear Layout  (Center)                  ║
      ║    ╔══════════════════════════════════════════════╗    ║   
      ║    ║ ImageView    ╔══════════════╗                ║    ║
      ║    ║              ║              ║    (Center)    ║    ║
      ║    ║              ║ Actual image ║                ║    ║
      ║    ║              ║              ║                ║    ║
      ║    ║              ║  (Center)    ║                ║    ║
      ║    ║              ║              ║                ║    ║
      ║    ║              ╚══════════════╝                ║    ║
      ║    ╚══════════════════════════════════════════════╝    ║ 
      ║                                                        ║
      ╚════════════════════════════════════════════════════════╝

And I want to do this:

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

Note that : imageview has gravity center with Linear Layout
I want the image in the imageview has gravity top, i try to set gravity:top, but nothing change. Help me

Edit: this is my 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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

执笔绘流年 2024-11-10 20:25:44

在 XML 中将 scaleType 设置为 fitStart

<ImageView
    android:id="@+id/ivMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitStart" >
</ImageView>

Set the scaleType to fitStart in your XML:

<ImageView
    android:id="@+id/ivMap"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitStart" >
</ImageView>
雪若未夕 2024-11-10 20:25:44

您可以使用ImageView.ScaleType.MATRIX并使用ImageView.setImageMatrix()设置图像矩阵。 Matrix可以使用Matrix.setRectToRect()方法获取。

You can use ImageView.ScaleType.MATRIX and set image matrix using ImageView.setImageMatrix(). Matrix can be obtained using Matrix.setRectToRect() method.

冷月断魂刀 2024-11-10 20:25:44

请检查 Terel 的答案,据说这是正确的答案。

根据布局中的其他内容,解决方案可能是使用其他布局(即 FrameLayout相当轻)代替 ImageView,并用 ImageView 代替实际图像,其 android:layout_gravity="top" >。

Please check Terel's answer, which is said to be the correct answer.

Depending on what else is in the layout, a solution could be to use another layout (i.e. a FrameLayout which is rather light) in place of the ImageView, and have the ImageView in place of the actual image, with its android:layout_gravity="top".

萌无敌 2024-11-10 20:25:44

您是否尝试过使用属性 android:gravity = "top"
我发现您的 ImageView 正在使用 android:layout_gravity 也许您可以尝试使用另一个。
据我所知,“android:layout_gravity”属性用于设置其父级元素的重力,而“android:gravity”属性用于设置设置元素内容的重力。 (如果我错了,请纠正我!=P嘿嘿!)

Have you tried using the attribute android:gravity = "top"
I see that for your ImageView you are using android:layout_gravity maybe you could try using the other one.
As far as I know, the "android:layout_gravity" attribute is used for setting the gravity of the element within its parent, while the "android:gravity" attribute is for setting the gravity of the element's contents. (Someone correct me if I'm wrong! =P hehe!)

内心荒芜 2024-11-10 20:25:44

使用 RelativeLayout 不是更容易吗?使用 android:layout_alignParentTop="true"android:layout_centerHorizo​​ntal="true" 就可以达到想要的效果。

Wouldn't it be easier to use a RelativeLayout? With android:layout_alignParentTop="true" and android:layout_centerHorizontal="true" you can achieve the desired effect.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文