如何从Android中的片段布局的看法? [英] How to get a view from a fragment layout in android?

查看:220
本文介绍了如何从Android中的片段布局的看法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的XML

I have an xml like this

activity_loginscreen.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:layout_height="match_parent"
    tools:context="android.arin.LoginScreen"
    tools:ignore="MergeRootFrame" />

然后gragment_loginscreen.xml

and then gragment_loginscreen.xml

<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>

在我的java文件我有

In my java file I have

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

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

    ImageView bubbles = (ImageView) findViewById(R.id.bubbles);
}

但泡沫最终是null,因为它找不到它,因为它看在活动XML之一,但真的是ImageView的在片段之一,我怎样才能得到它看起来呢?

But bubbles ends up being null because it can't find it because its looking in the activity xml one, but really the imageview is in the fragment one, how can I get it to look there?

感谢

推荐答案

请您 PlaceholderFragment 片段 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;
}

和使用 getActivity()片段上下文

and used getActivity() as a Context in Fragment like

Animation animContentUp = AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up_service);

这篇关于如何从Android中的片段布局的看法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆