【问题标题】:How to reference TextView in RelativeLayout如何在 RelativeLayout 中引用 TextView
【发布时间】:2013-08-12 19:49:58
【问题描述】:

这里还是个菜鸟……

我正在尝试渲染足球比赛的规格(球队、时间、锦标赛等)

我已经创建了一个 team.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >

<ImageView
    android:id="@+id/teamImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="2dp"
    android:adjustViewBounds="true"
    android:src="@drawable/sofabold_launcher"
    android:layout_alignWithParentIfMissing="false"
    android:clickable="false"
    android:cropToPadding="true"
    android:contentDescription="@string/teamLogoContentDescription"/>

<TextView
    android:id="@id/teamName"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/teamName"
    android:textSize="14sp"
    android:textStyle="bold"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/teamImageView"/>

</RelativeLayout>

一个teams.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="bottom"
android:orientation="vertical" >

<include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/team"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:id="@+id/homeTeam"/>

<include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/team"
        android:layout_below="@id/homeTeam"
        android:id="@+id/awayTeam"/>
</RelativeLayout>

然后我有一个 matchLayout.xml,包括我的 teams.xml

我最初的尝试是创建一个具有不同元素的大布局,并在我的 matchHolder 中通过它们的 id 引用它们,并使用类似

的东西填充它们
matchHolder.homeTeamName.setText(match.getHomeTeamName());

现在我尝试将我的 populateMatchHolder 更改为:

private void populateMatchHolder(View convertView, BaseMatchHolder matchHolder) {
    matchHolder.teams = (RelativeLayout) convertView.findViewById(R.id.teams);
}

现在我不知道如何在另一个RelativeLayout(匹配)中引用RelativeLayout(团队)中的TextView(主队名称)。

我是否完全错过了这里的重点?我需要在某处有一个名为 homeTeamName 的 id 吗?和 awayTeamName?拥有两次 teamName 还不够吗?一次包含在 team.xml 的 homeTeam 中,一次包含在 team.xml 的 awayTeam 中。

希望我的意思有点清楚 :-) 已经很晚了,我累了 :-/

提前致谢

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    如果您想通过 id 引用视图,该 id 在给定范围内应该是唯一的,否则 android 将仅返回具有给定 id 的第一个视图。

    因此,如果您执行类似teamsView.findViewById(R.id.teamName) 的操作,您只会获得 homeTeam 布局的 teamName 视图,因为在 teams.xml 中有两个具有相同 id 的视图(homeTeam 包含一个视图,awayTeam 包含一个视图)。

    解决方案:先获取每个团队的团队布局,然后设置团队名称 例如:

    View homeTeam = teamsView.findViewById(R.id.homeTeam);
    TextView homeTeamName = homeTeam.findViewById(R.id.teamName);
    homeTeamName.setText("blablabal");
    //the same for other team
    

    顺便说一句。使用android:id="@+id/teamName" 而不是android:id="@id/teamName"

    【讨论】:

    • 所以我应该回去让我的 matchHolder 指定 homeTeamName 和 awayTeamName,然后通过首先找到球队布局,然后是 homeTeam 布局,最后是名称来定义它们?
    • 是的,我认为如果您想通过将team.xml 布局包含在teams.xml 布局中来重用它,您应该采用这种方式。
    • 谢谢!我回家后会去(或者也许 tmrw)
    【解决方案2】:

    您可以使用该 textView 的 id 来引用它。例如

    <TextView
        android:id="@+id/teamName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/teamName"
        android:textSize="14sp"
        android:textStyle="bold"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@+id/teamImageView"/>
    

    您可以通过 id teamName 引用它。当您在另一个布局中使用包含标签时,它会成为该父布局的一部分。

    还有一个用途 use android:id="@+id/teamName" 而不是 android:id="@id/teamName" 因为每当您第一次在布局中使用该特定 id 时,它必须与 @+id 一起使用,以后您可以简单地在布局中的任何位置使用 @id 来引用该特定视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2015-11-02
      • 2014-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多