【问题标题】:How to know the View Elements after loading a layout?加载布局后如何知道视图元素?
【发布时间】:2015-05-15 08:36:52
【问题描述】:

我正在尝试编写代码,在加载布局后,我想知道哪些视图元素与布局相关,如 TextView、EditText、Checkbox 等。

我的代码:

MainActivity.java

package com.example.myview;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;


public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
    // How to get to know about the UI Elements related to this layout //

    }


}

demo_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

 <LinearLayout 
           android:id="@+id/lnr"    
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#ddccee"
           android:orientation="vertical">

        <TextView 
            android:id="@+id/txt1"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="Test Layout"
            android:layout_gravity="center"
            android:gravity="center"/>

        <EditText 
            android:id="@+id/edit1"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="Test Layout"
            android:layout_gravity="center"
            android:gravity="center"/>


        <TextView 
            android:id="@+id/txt2"
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:textSize="15dp"
            android:textColor="#ffffff"
            android:text="Test Layout"
            android:layout_gravity="center"
            android:gravity="center"/>


</LinearLayout>

此布局有 TextView 、 EditText ,所以,我想知道如何以编程方式获取与此布局相关的 UI 元素。

所以,请给我一些解决方案。

【问题讨论】:

  • 您需要初始化所有 UI,并提供膨胀视图对象的引用。

标签: android android-layout view layout-inflater


【解决方案1】:

您可以使用getChildCountREF 获取添加到ViewGroup 的视图计数,然后使用getChildAt REF 获取给定索引处的View

您正在使用 LinearLayout 作为基本布局,它已经扩展了 ViewGroup 只需更改以下行。

    LinearLayout view = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.demo_layout, null);

【讨论】:

  • 是的,谢谢,但是如何在 Activity 中使用 ViewGroup...
  • 是的,我们可以这样做,所以我们需要有 CustomView 类
  • 是的,我做了 view.getChildCount() ,所以我得到的价值是 1 ,但我认为它应该是 3 对...,请让我看看有什么问题
  • 您的主要 LinearLayout 有一个子布局,因此如果您在此布局中获得三个子布局,则值为 1
  • 很好,我明白了,但是如果我想考虑内部的 LinearLayout,我怎样才能得到孩子的数量......
【解决方案2】:

在你给你的demo_layout 充气后,只需像这样在你的view 实例上调用findViewById

View view = LayoutInflater.from(this).inflate(R.layout.demo_layout, null);
TextView txt1 = (TextView) view.findViewById(R.id.txt1);

【讨论】:

  • 哦!伙计,这个东西每个 Android 开发人员都知道,我想知道,如何以编程方式知道布局中有一个 Textview
  • 是的,没问题,可能你只是误解了这个问题,但不用担心,请你给我一些解决方案
【解决方案3】:

view 的元素必须由view.findViewById() 检索。 除了在活动中,您直接使用 findViewById() 来检索您在 setContentView() 中设置的 contentView 的视图元素。

【讨论】:

    猜你喜欢
    • 2017-02-12
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多