【问题标题】:How to access children of included layout in XML?如何访问 XML 中包含布局的子级?
【发布时间】:2019-09-10 08:13:02
【问题描述】:

我有自定义图标化按钮,我想在我的项目中使用它,但我需要访问它的子元素以在 XML 中设置文本。如何做到这一点?

<include
    android:id="@+id/leaveBtn"
    layout="@layout/iconified_button"
    style="@style/button_rounded_colorized"
 />

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/button_rounded_colorized">

    <ImageView
        android:id="@+id/buttonIcon"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="@dimen/padding_small"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:src="@drawable/arrow" />

    <TextView
        android:id="@+id/buttonText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Text"
        android:textAllCaps="true"
        android:textColor="@color/colorText"/>

</LinearLayout>

我还希望能够更改包含布局中的按钮样式。我不想在 iconified_button 布局 xml 中使用静态样式。

更新:

制作自定义类扩展LinearLayout上方的膨胀布局,并通过attrs.xml中的自定义参数设置Button文本和ImageView图标,这些参数可以在XML中访问。

<com.project.utils.IconifiedButton
   android:id="@+id/leaveBtn"
   style="@style/button_rounded_colorized"
   app:buttonText="@string/button_text_2"
   app:buttonIco="@drawable/arrow"
 />

attrs.xml

<resources>
    <declare-styleable name="IconifiedButton">
        <attr name="buttonText" format="string" />
        <attr name="buttonIco" format="integer" />
    </declare-styleable>
</resources>

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    使用Button button = findViewById(R.id.leaveButton)(我不确定R.id.leaveButton是否足够,或者你必须为你的LinearLayout设置id)然后使用button.findViewById(R.id.buttonText)去你的TextView

    【讨论】:

      【解决方案2】:

      这不是你要找的。
      要拥有 "iconified Button",您可以使用带有 app:icon 属性的简单 MaterialButton,而不是 2 个视图:

          <com.google.android.material.button.MaterialButton
              style="@style/Widget.MaterialComponents.Button.Icon"
              app:icon="@drawable/ic_add_24px"
              android:text="Button"
              ../>
      

      您可以使用简单的设置文本:

      MaterialButton button = findViewById(R.id.xxxx);
      button.setText("...");
      

      您可以在运行时自定义样式(背景颜色、文本颜色、图标颜色和可绘制、描边、圆角半径)。详情请查看官方文档。

      在您的示例中,只需使用:

      TextView textview = findViewById(R.id.buttonText);
      textview.setText("....");
      

      【讨论】:

      • MaterialButton 库由于某种原因不适合我。缺少样式。但我想通了。我制作了扩展 LinearLayout 的类并膨胀了这个 xml,向attrs.xml 添加了自定义属性,现在我可以在 xml 中访问和设置子属性。
      • 缺少样式?你的意思是你已经切换到材料主题?在任何情况下,您也可以使用标准 Button 中的 android:drawableStart="" 属性来实现相同的行为
      • 是的,drawableStart 不太好,因为图标被推到按钮的最边缘,您必须手动添加填充以将其定位到中心。带有布局的自定义类是目前最好的,但谢谢
      猜你喜欢
      • 1970-01-01
      • 2017-05-01
      • 2016-12-20
      • 2011-05-02
      • 2021-05-16
      • 2011-08-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多