如何将 RelativeLayout 作为变量访问?
how to get access RelativeLayout as a variable?
我不知道如何以编程方式访问我的 RelativeLayout 以便我可以在 Handler 中调用诸如 relativeLayout.getChildCount();
之类的方法。
编辑 - 好吧,如果我想获得我的 RelativeLayout 中的视图总数,我会调用什么函数?
在文件夹 res/layout 中,我假设您的布局名为 layout.xml
确保你的 RelativeLayout 有 android:id 段
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myRelativeLayout" > //the id name can be acsessed in activity
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
</RelativeLayout>
访问 activity
中的 RelativeLayout id
public class mainActivity extends Activity {
//make variable globally
RelativeLayout myRelativeLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//the layout file
setContentView(R.layout.layout);
//access the RelativeLayout and initialiszed the object
myRelativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout);
myRelativeLayout.getChildCount();
}
}
我不知道如何以编程方式访问我的 RelativeLayout 以便我可以在 Handler 中调用诸如 relativeLayout.getChildCount();
之类的方法。
编辑 - 好吧,如果我想获得我的 RelativeLayout 中的视图总数,我会调用什么函数?
在文件夹 res/layout 中,我假设您的布局名为 layout.xml
确保你的 RelativeLayout 有 android:id 段
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myRelativeLayout" > //the id name can be acsessed in activity <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </RelativeLayout>
访问 activity
中的 RelativeLayout idpublic class mainActivity extends Activity { //make variable globally RelativeLayout myRelativeLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //the layout file setContentView(R.layout.layout); //access the RelativeLayout and initialiszed the object myRelativeLayout = (RelativeLayout) findViewById(R.id.myRelativeLayout); myRelativeLayout.getChildCount(); } }