【问题标题】:Android - My own Tile - ImageButton + TextFieldAndroid - 我自己的 Tile - ImageButton + TextField
【发布时间】:2017-02-18 18:10:48
【问题描述】:

我的问题是关于构建我自己的 ImageButton。 我想拥有所有具有 ImageButton 的东西,但我想在上面添加一些 TextField。 所以我想创建具有:

  1. 正方形大小
  2. 图片在顶部,宽度为 100%,高度为 80%
  3. 底部的文本字段:100% 宽度,20% 高度
  4. 所有方形“平铺”都必须是可点击的,并且可以正常工作

类似这样的:

如何做到这一点? 如果您可以为此提供一些代码,那将是非常非常好的,但在最低限度的选择中,我将非常感谢一些“待办事项列表”,我应该逐步做什么。

【问题讨论】:

  • 必须是ImageButton 吗?难道你不能只用 XML 设计它并将OnClickListener 添加到Layout 吗?
  • @JonZarate 是的,这可能是一个很好的解决方案

标签: android


【解决方案1】:

您可以使用以下布局:对于 80% 和 20% 高度使用 android:layout_weight 属性

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout_id"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".80"
        android:src="@mipmap/ic_launcher"
        android:text="left" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".20"
        android:gravity="center"
        android:text="Sample Text" />

</LinearLayout>

【讨论】:

  • 好的,谢谢,看起来不错。您建议在 .java 中做什么?为这个线性布局创建带有控制器的java文件,然后在这个或什么上实现监听器?
  • 您可以在网格适配器的 getView() 方法中扩展布局并将点击侦听器添加到整个 LinearLayout
【解决方案2】:

基本上我们正在制作一个相对布局(父级),这将是我们的“图像按钮”。下面是 XML 代码,对于 Java,您只需要在此 id 上实现 setonclicklistener。

<RelativeLayout
    android:layout_width="wrap_content"
    android:id="@+id/imageButton"
    android:layout_height="wrap_content">

    <ImageView
        android:id="@+id/page_image"
        android:layout_marginRight="6dip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:src="@drawable/no_photo" />
    <TextView
        android:id="@+id/page_name"
        android:layout_alignTop="@id/page_image"
        android:layout_toRightOf="@id/page_image"
        android:layout_below="@id/page_image
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

【讨论】:

  • clickListener 必须在某个“mainActivity.java”类上实现,否则最好将另一个类“myComponent.java”创建为一个带有监听器的图块的控制器,然后将其导入主要活动?好的做法是什么?
  • 创建一个类并扩展相对布局只是为了覆盖单个 OnClickListener 方法会增加开销。您可以在此相对布局中创建一个对象并在其上实现 onClickListener。
猜你喜欢
  • 2012-04-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多