【问题标题】:Set a TextView text without id under a relativeLayout with id在带 id 的 relativeLayout 下设置不带 id 的 TextView 文本
【发布时间】:2014-09-21 12:29:45
【问题描述】:

是否可以在带有 id 的相对布局下访问没有 id 的 textview 来更改其文本?

这里是一个示例代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/backFace"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    >
<TextView
    android:gravity="center_horizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back"
    />
 </RelativeLayout>

【问题讨论】:

    标签: android textview android-relativelayout settext


    【解决方案1】:

    为了操作视图,您需要获取对它的引用,这是通过 findViewById 方法完成的。因此,如果您没有为特定视图提供任何 ID,那么无论布局的层次结构如何,您都不会有引用。

    但令人惊讶的是,这种情况有一种解决方法。由于 RelativeLayout 扩展了 ViewGroup 具有 getChildCount() 和 getChildAt(int index) 方法,您可以试试这个:

       TextView tv_child= (TextView)relativeLayout.getChildAt(i); // i = index of the textview
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2012-10-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 1970-01-01
      相关资源
      最近更新 更多