我有一个活动LoginActivity
和一个片段GoogleSignInFragment
。该应用程序的登录逻辑运行得很好,但唯一困扰我的是GoogleSignInFragment
没有出现在activity_login.xml
的XML预览中。如果没有它出现,我将很难将我的代码移动到Fragments,因为我再也不能对活动进行风格化了
以下是所有相关代码。。。
LoginActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.sign_in_google, new GoogleSignInFragment())
.commit();
}
activity_login.xml
<FrameLayout
android:id="@+id/sign_in_google"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_sign_in_google_button"/>
GoogleSignInFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_google_sign_in_button, container, false);
return v;
}
fragment_google_sign_in_button.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
tools:context=".GoogleSignInFragment">
<com.google.android.gms.common.SignInButton
android:id="@+id/sign_in_google_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal" />
</FrameLayout>
有时Android Studio无法加载预览,因此您可以尝试文件选项中的无效缓存/重新启动。
为此,请转到文件>单击使缓存无效/重新启动
https://developer.android.com/studio/write/tool-attributes#toolslayout
说它是用于:<fragment>
,但你使用的是FrameLayout
我认为它们对您的代码没有问题,因为您的片段activity_login.xml不包含任何内容,并且fragment_google_sign_in_button.xml包含com.google.android.gms.commun.SignInButton是在外部库的帮助下使用的,因此有时不会显示预览。