【发布时间】:2016-12-20 04:38:13
【问题描述】:
我正在尝试从包含的布局中访问TextView,但我不知道如何。
这是我setContentView(R.layout.activity_home);我onCreate上的布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_home"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="@menu/activity_home_drawer" />
</android.support.v4.widget.DrawerLayout>
可以看到这个布局包含了另外一个布局,app_bar_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.branco.sauce.home">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_home"
android:id="@+id/includeContentHome"/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
现在这个其他布局包括我的最终 xml 文件,我有一个 TextView,content_home.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.branco.sauce.home"
tools:showIn="@layout/app_bar_home"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ROLA 3"
android:textSize="100px"
android:id="@+id/txtRola3"
/>
<ListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
我想要的是通过以下方法访问id为txtRola3的TextView:
@Override
protected void onPostExecute(Void result) {
final LayoutInflater inflateContentHome = getLayoutInflater();
final View viewContentHome = inflateContentHome.inflate(R.layout.app_bar_home, null);
TextView txtTest = (TextView)viewContentHome.findViewById(R.id.includeContentHome).findViewById(R.id.txtRola3);
txtTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("You clicked on the textview");
}
});
}
如您所见,我尝试过对一些布局进行充气,但我不知道如何实现这一点。我的意思是,我可以看到 TextView 和它的文本,但 listener 不起作用。我确实这样做是为了学习和访问我的ListView(现在我无法填充数据)。我知道以前已经回答过这类问题,但由于我有三种布局,其中一种包含另一种,所以我无法得到其他答案的任何运气。
编辑:home.java:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
new RemoteDataTask().execute();
}
private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
protected void onPostExecute(Void result) {
final LayoutInflater inflateContentHome = getLayoutInflater();
final View viewContentHome = inflateContentHome.inflate(R.layout.app_bar_home, null);
TextView txtTest = (TextView)viewContentHome.findViewById(R.id.includeContentHome).findViewById(R.id.txtRola3);
txtTest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("You clicked on the textview");
}
});
}
}
OBS:我的代码大约有 800 行,所以我在这里添加了最重要的部分。
【问题讨论】:
-
可以使用
添加不同的xml -
我不明白你的意思,@KrishnaJ
-
您没有在 中指定布局文件 -
@BrunoBrandãoBorges 您可能需要在
onCreate或onCreateView中找到您的视图,然后您可以在代码中的任何位置使用它。 -
@KrishnaJ,我已经包含了布局。我想要的是访问 Itens 中的。
标签: android xml layout view inflate