简体   繁体   English

如何在 ImageButton 中添加多个 TextView?

[英]How add multiple TextView inside an ImageButton?

Is it possible to add multiple TextView inside one ImageButton with colour background ?是否可以在一个带有颜色背景的 ImageButton 中添加多个 TextView ?

The core need is to have a button with the action text on it, and a subtext nearby explaining the action or giving other information related to the action.核心需要是有一个带有动作文本的按钮,以及一个解释动作或提供与动作相关的其他信息的潜台词。 This subtext can vary from time to time.这个潜台词可能会不时变化。

Considering this requirement, one solution is to have a normal button and a subtext below, not clickable.考虑到这一要求,一种解决方案是在下方设置一个普通按钮和一个潜台词,不可点击。 But I find it messy.但是我觉得很乱。 A better approach which I like is, on iOS for instance, to have a clickable UIView containing the action as bold text and the explanation as light text.我喜欢的一个更好的方法是,例如在 iOS 上,有一个可点击的 UIView,其中包含粗体文本的操作和浅文本的解释。 See the image bellow containing 4 buttons :请参阅下面包含 4 个按钮的图像: iOS 插图

How to achieve the same on Android with Java ?如何使用 Java 在 Android 上实现相同的目标? The closest I can have is to have an ImageButton bellow a TextView, and it does not sound right.我能拥有的最接近的是在 TextView 下方放置一个 ImageButton,这听起来不太对。

Is possible to nest TextViews inside an ImageButton ?可以在 ImageButton 内嵌套 TextViews 吗? If not, what is the best alternative ?如果没有,最好的选择是什么?

I hope this may be useful it explains how to position a textView within and in front of a imageView in the XML.我希望这可能是有用的,它解释了如何在 XML 中的 imageView 内部和前面放置 textView。

TextView inside of ImageButton/ImageView XML - Android Dev ImageButton/ImageView XML 中的 TextView - Android Dev

Obviously make sure each view has a unique id/name which you can assign as shown here on this link显然,请确保每个视图都有一个唯一的 ID/名称,您可以按照此链接的此处所示进行分配

Sorry I cannot explain specifically myself but it has been a while since developing in Java for Android.抱歉,我自己无法具体解释,但自从用 Java 为 Android 开发以来已经有一段时间了。

I dont know why you want this behaviour but you can make a container for your views and add a click listener to the whole view.我不知道你为什么想要这种行为,但你可以为你的视图创建一个容器并向整个视图添加一个点击监听器。 you can also use it anywhere.你也可以在任何地方使用它。

an example of this would be.这方面的一个例子是。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent" 
android:background="@drawable/container_background"
android:layout_height="wrap_content">

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0.33"
  />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0.33"
    />

<TextView
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_margin="5dp"
    android:layout_weight="0.33"
  />

add a selector background添加选择器背景

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/text_pressed" />
<item android:color="@color/normal" />
</selector>

and the listener和听者

 findViewById(R.id.container).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

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

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