java - 片段显示在我的手机上,但不在 XML 预览中
问题描述
我有一个活动LoginActivity
和一个片段GoogleSignInFragment
。该应用程序的登录逻辑工作得很好,但唯一让我烦恼的是GoogleSignInFragment
没有出现在activity_login.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 无法加载预览,因此您可以尝试File选项中的Invalidate Cache / Restart选项。
为此,转到文件>单击无效缓存/重新启动
推荐阅读
- mysql - MYSQL 从组中获取总数
- apostrophe-cms - TypeError:无法读取未定义的属性“位图”(ApostropheCMS)
- xamarin.forms - 在 xamarin 表单中异步加载数据的正确方法
- c++ - c++中0-x和-x的区别
- apache-kafka - 卡夫卡流:java.nio.file.DirectoryNotEmptyException
- java - 在 Intellij 和 Kotlin 中找不到 Log4j2 配置
- c++ - 这个 bst 代码有什么问题?当我输入第一个节点的数据时,它给了我错误并且不会继续
- list - 如何从博客的标签/类别列表中删除点?
- java - 如何修改 ArrayList 中与其他对象具有相似值的对象值
- r - 从 ggplot 创建的 plotly 图中的轴位置