【问题标题】:How to position a textView relative to an image in Android如何在Android中相对于图像定位textView
【发布时间】:2015-04-21 00:43:10
【问题描述】:

我正在为汽车开发一个应用程序,用户可以在其中注册汽车的车牌号。在主屏幕中,我想显示带有用户车牌的汽车图像(这是一个 textView)。问题是,每当我为某些分辨率修复它时,它就会停止在其他分辨率上工作,有谁知道如何解决这个问题?

这是我的 xml 代码:

<FrameLayout 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" tools:context="com.bsbapps.happycars.HomeFragment">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <Button
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/notify"/>
        <Button
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="callSubscribe"
            android:text="@string/subscribe"/>
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="bottom"
        android:gravity="center">
        <ImageView
            android:id="@+id/car_image"
            android:adjustViewBounds="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/car"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="jfk-4548"
            android:textStyle="bold"
            android:layout_marginBottom="30dip"
            android:layout_alignBottom="@id/car_image"
            android:layout_centerHorizontal="true"/>


    </RelativeLayout>

</LinearLayout>

如果有人想准确复制我所拥有的,这里是图片:

我得到的结果是(在 Nexus 9 上):

并且(在 Nexus 5 上):

【问题讨论】:

    标签: android xml imageview android-relativelayout


    【解决方案1】:

    问题是,每当我为某些分辨率修复它时,它就会停止在其他分辨率上工作,有谁知道如何解决这个问题?

    如果你想做基于维度的,你应该使用multiple "dimens" files

    改变

    android:layout_marginBottom="30dip"
    

    android:layout_marginBottom="@dimen/someDimensionName"
    

    然后在名为values-&lt;screen-resolution&gt;的资源文件夹下的多个dimens.xml文件中声明someDimensionName

    values/dimens.xml(默认)

    <dimen name="someDimensionName">30dp</dimen>
    

    values-sw600dp/dimens.xml(大屏)

    <dimen name="someDimensionName">60dp</dimen>
    

    (确切的values-size 会有所不同;这只是一个示例。有关更多详细信息/尺寸选项,请参阅the documentation。)

    【讨论】:

    • 感谢您的详细解释。我希望有一个更简单的方法。例如,在 Unity 中,您可以为对象设置父级,无论缩放如何,子级都将保持比例。但无论如何,我会听从你的建议!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多