【问题标题】:How to programatically change an attribute of widget created by a LayoutInflater?如何以编程方式更改由 LayoutInflater 创建的小部件的属性?
【发布时间】:2013-01-04 13:04:44
【问题描述】:

我想访问由 LayoutInflater 创建的小部件的子级,以更改其中的 TextView 显示的文本。 我怎么能这样做? 我做了一个图表来解释。

LinearLayout
--->LayoutInflater

    ---->LinearLayout
         ---->LinearLayout
              ---->TextView
              ---->TextView
    ---->LinearLayout
         ---->LinearLayout
              ---->TextView
              ---->TextView
    ---->LinearLayout
         ---->LinearLayout
              ---->TextView
              ---->TextView
etc

我希望能够访问每个单独的 TextView 并更改其属性。

【问题讨论】:

    标签: android android-layout android-linearlayout layout-inflater


    【解决方案1】:

    您可以通过充气器返回的View 访问 TextView 并更改其属性。

    我假设你有这样的东西:

    View view = inflater.inflate(R.layout.my_layout, null);
    

    然后你可以像这样访问资源:

    TextView text = (TextView)view.findViewById(R.id.textView1);
    text.setText("Hello");
    

    【讨论】:

    • 那么 findViewById 只返回调用它的视图的子视图?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 2020-05-03
    • 2012-07-10
    • 2012-02-29
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    相关资源
    最近更新 更多