如何在android中从片段布局获取视图?
我有一个这样的xml activity_loginscreen.xml如何在android中从片段布局获取视图?,android,android-fragments,Android,Android Fragments,我有一个这样的xml activity_loginscreen.xml <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layo
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="android.arin.LoginScreen"
tools:ignore="MergeRootFrame" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/LoginView"
tools:context="android.arin.LoginScreen$PlaceholderFragment" >
<ImageView
android:id="@+id/bubbles"
android:contentDescription="@string/bubbles_cd"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:alpha=".75"
android:src="@drawable/bubbles" />
</RelativeLayout>
但是泡泡最终是空的,因为它无法找到它,因为它在ActivityXMLOne中查找,但实际上imageview在FragmentOne中,我如何让它在那里查找
谢谢将图像视图放在
占位符片段
onCreate视图
中,并在占位符片段
中充气布局gragment\u loginscreen,这将生成一个视图
示例:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gragment_loginscreen, container, false);
ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles);
return view;
}
使您的
占位符片段片段onCreateView(..)
类似
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gragment_loginscreen, container, false);
ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles);
return view;
}
Animation animContentUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up_service);
并将getActivity()
用作片段中的上下文
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gragment_loginscreen, container, false);
ImageView bubbles = (ImageView)view.findViewById(R.id.bubbles);
return view;
}
Animation animContentUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up_service);
@omega您确实开始处理一个片段,因此您应该在片段类(占位符片段)中而不是在活动类中进行处理。如果我从中获得气泡,那么如何获得用于此Animation Animation contentup=AnimationUtils.loadAnimation的正确上下文(context,R.anim.slide\u up\u服务)代码>在占位符片段中创建aconstructor并传递活动的文本。。对不起,我不能在下面的回答中回答