【问题标题】:Keep fixed margin between buttons android保持按钮之间的固定边距android
【发布时间】:2014-03-01 19:14:42
【问题描述】:

我正在开发一个应用程序并遇到这样的情况:

我有 2 个来自图像的按钮,它们必须适合整个屏幕。 所以我使用了线性布局并将 layout_weight 都设置为 1。 然而,正如你从我的草图中看到的那样,它们不是矩形的,所以右边的那个必须有负的左边距才能在草图上看起来像。但是图像是动态缩放的,因此边距量取决于屏幕尺寸。我尝试为 ldpi、mdpi、hdpi 等创建相乘的尺寸文件,但只在预览 2 hdpi 屏幕中查看它看起来不一样。知道原始尺寸边距有没有办法在所有屏幕上正确缩放?

问候

编辑: Ofc 我正在使用 dp 单位。但就像我说的那样,对于所有具有相同 dpi 的屏幕来说,情况并不相同。 例如:

值-xhdpi/dimens.xml:

<resources>

    <dimen name="button_margin">-23dp</dimen>

</resources>

Nexus 10 2560x1600 xhdpi:

Galaxy Nexus 720x1280 xhdpi:

还有我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:orientation="vertical" >

<LinearLayout 
     android:id="@+id/riderstabs"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:orientation="horizontal">

        <ImageView
            android:id="@+id/btn1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal" 
            android:checked="true"
            android:layout_weight="1"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/btn1" />

        <ImageView
            android:id="@+id/btn2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="center_horizontal" 
            android:layout_weight="1"
            android:layout_marginLeft="@dimen/button_margin"
            android:scaleType="fitXY"
            android:adjustViewBounds="true"
            android:src="@drawable/btn2" />

</LinearLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/list"
    android:background="@android:color/transparent"
    android:layout_weight="7"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</android.support.v4.view.ViewPager>

</LinearLayout>

编辑:

我发现还有另一类屏幕尺寸。

正如文档所说: 尺寸:小、正常、大和超大 密度:ldpi(低)、mdpi(中)、hdpi(高)和 xhdpi(超高)[我认为还有 xxhdpi、xxxhdpi 和 tvhdpi]。

所以我有 2 个按钮:第一个是 262x46,第二个是 404x46。 -26 像素是第二个按钮完美匹配的原始边距。 (所以总和是 404+262-26=640)。

我发现了一些影响密度的因素: 低密度脂蛋白 - 0.75 mdpi - 1.0 hdpi - 1.5 xhdpi - 2.0 xxhdpi - 3.0 xxxhdpi - 4.0

但是,例如,当我猜测 mdpi 的右边距并将 hdpi 乘以 1.5 时,它没有正确缩放(至于在 Eclipse 中查看预览)。并且有屏幕尺寸,所以它更加复杂。

【问题讨论】:

  • 您的意思是边距金额取决于屏幕密度吗?在我看来,如果您使用 dp 作为边距,您可以轻松实现您所描述的

标签: android android-layout


【解决方案1】:

使用marginpadding 的密度无关点 (dp) 单元是在所有设备尺寸上保持绝对缩小的最佳方法,如下所示:

android:layout_margin="20dp"

确保对所有 XML 元素使用 dp,对所有文本使用 sp

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-31
    • 2012-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    相关资源
    最近更新 更多