gpt4 book ai didi

java - 使用 fragment 显示 Activity 中的工具栏时出现问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:10:46 24 4
gpt4 key购买 nike

您好,我在使用 fragment 显示工具栏时遇到问题。我正在将我的应用程序与 Facebook 集成。我能够连接到 Facebook 并获取基本的用户个人资料详细信息,例如名字、姓氏和用户个人资料图片。现在我将信息从主要 Activity 传递到第二个 Activity 。我的第二个 Activity 将是使用 fragment 。现在我制作了一个包含工具栏、用户名和图片的 fragment 。但我在显示 fragment 时遇到问题。有人会告诉我我做错了什么吗?谢谢。以下是我的文件。

颜色.xml:

<resources>
<color name="toolBar">#00ff80</color>
<color name="colorPrimary">#cc3300</color>
<color name="colorPrimaryDark">#C51162</color>
<color name="textColorPrimaryDark">#FFFFFF</color>
<color name="windowBackground">#FFFFFF</color>
<color name="navigationBarColor">#808080</color>
<color name="colorAccent">#FF80AB</color>
<color name="backgroundColor">#003399</color>
</resources>

样式.xml:

<resources>

<!-- Base application theme. -->
<!-- <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> -->
<!-- Customize your theme here. -->
<!-- </style> -->
<!-- Base Application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

</style>

</resources>

activity_second.xml:

<LinearLayout
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:orientation="vertical"
android:background="@color/backgroundColor"
>
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="@layout/first_fragment"
android:name="edu.sjsu.cmpe277.termproject.Fragments.FirstFragment"
android:id="@+id/toolBar_fragment"
/>

</android.support.v4.widget.DrawerLayout>
</LinearLayout>

first_fragment.xml:

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

<include
android:id="@+id/toolBar"
layout="@layout/toolbar" />

<TextView
android:id="@+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/users_FirstName"
android:textSize="@dimen/font_size"
android:hint="JJJJ"
android:layout_gravity="center_horizontal"/>

<com.facebook.login.widget.ProfilePictureView
android:id="@+id/profilePic"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
>
</com.facebook.login.widget.ProfilePictureView>

</LinearLayout>

工具栏.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
android:elevation="5dp"
android:gravity="top|start">

</android.support.v7.widget.Toolbar>

FirstFragment.java:

public class FirstFragment extends Fragment implements FragmentCommunicator {

private ProfilePictureView profilePictureView;
private Toolbar toolbar;
private TextView textView;
private FragmentCommunicator fragmentCommunicator;
// private GetDetails getdetails;
//private DrawerLayout drawerLayout;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.event_fragment, container, false);
toolbar = (Toolbar)rootView.findViewById(R.id.toolBar);
textView = (TextView)rootView.findViewById(R.id.textName);
profilePictureView = (ProfilePictureView)rootView.findViewById(R.id.profilePic);

return rootView;
}

@Override
public void onAttach(Context context) {
super.onAttach(context);
try{
fragmentCommunicator =(FragmentCommunicator)context;
}
catch(ClassCastException ex) {
throw new ClassCastException(ex.toString());
}

}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
toolbar = (Toolbar)getActivity().findViewById(R.id.toolBar);
profilePictureView = (ProfilePictureView)getActivity().findViewById(R.id.profilePic);

}

@Override
public void setDetails(String userId, String name) {

}

第二个Activity.java:

    public class secondActivity extends AppCompatActivity implements FragmentCommunicator{

private FirstFragment firstFragment;
private FragmentTransaction fragmentTransaction;
private Intent intent;


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

intent = getIntent();
String firstName = intent.getStringExtra("firstName");
String userId = intent.getStringExtra("Id");

firstFragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id.toolBar_fragment);
intent = getIntent();
String firstName = intent.getStringExtra("firstName");
String name = intent.getStringExtra("lastName");
String userId = intent.getStringExtra("Id");

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_second, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}


@Override
public void setDetails(String userId, String name) {

FirstFragment firstFragment = (FirstFragment)getSupportFragmentManager().findFragmentById(R.id.toolBar_fragment);
if(firstFragment !=null) {
firstFragment.setDetails(userId, name);
}
}

}

最佳答案

这里有几个要点供您引用:

  • 不要将其放入 fragment 布局中,从 Activity 中处理工具栏会更好、更智能。 (您仍然可以使用 fragment 中的选项菜单将组件添加到工具栏。

  • 在您的第二个 Activity xml 中阻止使用 ,将您的布局放在该 xml 的顶部,并在其上进行 fragment 事务的容器。

您的 fragment 应该只处理它们的用途,您的第二个 Activity 应该替换您的 fragment (进入容器)并处理工具栏。

祝你好运

关于java - 使用 fragment 显示 Activity 中的工具栏时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33973310/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com