简体   繁体   English

将图像按钮与背景android对齐

[英]Aligning a image button with a background android

I am pretty new to android programming and i was hoping you could help me out with something. 我是android编程的新手,我希望您能帮我一些忙。 I have set a background in an xml file and i am trying to align a image button to a specific area of my background. 我已经在xml文件中设置了背景,并且尝试将图像按钮与背景的特定区域对齐。 I have tried using density pixels however it is not precise enough and the button now covers an area of the background i would like to be visible. 我尝试使用密度像素,但是不够精确,按钮现在覆盖了我希望可见的背景区域。 Any ideas on how i could fix this? 关于如何解决此问题的任何想法?

Many thanks, 非常感谢,

Alex 亚历克斯

My code is as follows: 我的代码如下:

    <?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" 
    android:background="@drawable/home_background">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="93dp" >

        <ScrollView
            android:id="@+id/scrollView1"
            android:layout_width="match_parent"
            android:layout_height="341dp" >

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal" >

                <ImageButton
                    android:id="@+id/imageButton1"
                    android:layout_width="100dp"
                    android:layout_height="100dp"
                    android:layout_marginLeft="30dp"
                    android:layout_marginTop="30dp"
                    android:background="@null"
                    android:scaleType="fitXY"
                    android:src="@drawable/workbench" />

            </LinearLayout>
        </ScrollView>

    </LinearLayout>

</LinearLayout>

You can always try using px instead of dp , however the whole point of dp is that they will scale automatically for any screen. 你总是可以尝试使用px代替dp ,但是整点dp是,他们将自动缩放为任何屏幕。 If you use px I suspect that it will only work well on the current screen resolution that you're testing on. 如果您使用px我怀疑它只能在您正在测试的当前屏幕分辨率下正常工作。 You would have to define px values for numerous screen resolutions/densities to ensure that your app displayed correctly across different devices. 您必须为多种屏幕分辨率/密度定义px值,以确保您的应用在不同设备上正确显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM