【问题标题】:How to set images and text on a specific position in a widget?如何在小部件中的特定位置设置图像和文本?
【发布时间】:2015-09-24 13:13:02
【问题描述】:

我正在制作一个小部件,我想在特定位置设置文本和图像。我想要一个像 UCCW 小部件这样的输出。我尝试过框架布局、相对布局和画布,但没有什么比这更有效。每当我将小部件从 2x2 调整为像 4x2 这样的奇怪尺寸时,文本的位置就会混乱。我想知道如何将它们设置在特定位置以及如何在小部件大小更改时自动调整大小而不会出现任何问题。

图片供参考:

谢谢!

【问题讨论】:

    标签: android resize widget position


    【解决方案1】:

    LinearLayout 与可见视图和空白空间的权重一起使用。 这是一个例子:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/holo_red_dark"
        android:orientation="vertical">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center">
    
            <!--Placeholder (empty space) before.-->
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="3" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="100dp"
                android:layout_weight="10"
                android:background="@android:color/holo_blue_dark"
                android:gravity="center"
                android:text="Hello" />
    
            <!--Placeholder (empty space) after. -->
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="7" />
        </LinearLayout>
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="40dp"
            android:gravity="center">
    
            <!--Placeholder (empty space) before.-->
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="20" />
    
            <TextView
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:layout_weight="10"
                android:background="@android:color/holo_orange_dark"
                android:gravity="center"
                android:text="widget" />
    
            <!--Placeholder (empty space) after. -->
            <FrameLayout
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:layout_weight="6" />
        </LinearLayout>
    
    
    </RelativeLayout>
    

    小部件 2x2:

    小部件 4x2:

    【讨论】:

    • 感谢您的回复,感谢您的努力。但是我希望元素保持在它们的位置而不改变它们的宽度和高度。您可以参考 UCCW,该应用程序为文本、图像、弧线等提供 X、Y 位置。我想要同样的效果。我使用过画布,但无法为其中的文本获得相同的效果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-14
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多