【问题标题】:How do i display a textview over a button with custom layout?如何在具有自定义布局的按钮上显示文本视图?
【发布时间】:2016-09-13 08:00:55
【问题描述】:

如何在自定义按钮(使用 res/drawable 中的 png)上显示文本视图?

我正在尝试为 android 穿戴的可穿戴方形手表创建 UI。 我创建了自定义背景来更改我希望布局包含的两个按钮的外观。 当我在布局中添加一个 textview 时,它总是放在 Button 下,并且永远不可见。

我已经搜索过解决方案,但通常问题是人们使用了错误的布局。我已经尝试过框架和相对布局,它们都应该将其内容堆叠在一起,但没有。

虽然我可以在按钮本身中使用文本字段,但我宁愿有一个专用的 textview 用于写入里面的文本。但我就是想不通是什么问题。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Text"
        android:textColor="#ffffff"
        android:layout_gravity="center"/>

    <Button
        android:id="@+id/wearable_alarm_button"
        android:layout_width="wrap_content"
        android:background="@drawable/red_button"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="130dp"
        android:layout_height="120dp"
        android:background="@drawable/blue_button"
        android:id="@+id/wearable_timer_button"
        android:layout_gravity="right|bottom" />
</FrameLayout>

【问题讨论】:

标签: android android-layout android-studio wear-os


【解决方案1】:

标签后添加标签

<Button
    android:id="@+id/wearable_alarm_button"
    android:layout_width="wrap_content"
    android:background="@drawable/red_button"
    android:layout_height="wrap_content" />

<Button
    android:layout_width="130dp"
    android:layout_height="120dp"
    android:background="@drawable/blue_button"
    android:id="@+id/wearable_timer_button"
    android:layout_gravity="right|bottom" />
<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text"
    android:textColor="#ffffff"
    android:layout_gravity="center"/>

它将在按钮上方显示您的文本视图。

【讨论】:

  • 我不太清楚你的意思。你的意思是使用框架布局的堆叠属性吗?这也不能解决问题,我试过把它放在按钮之前和之后。并且将您的示例代码放入我的 xml 文件中也不能解决问题。
猜你喜欢
  • 1970-01-01
  • 2011-01-20
  • 2023-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多