如何在nexus7中修复android studio布局预览?

时间:2015-12-13 09:45:39

标签: android android-layout android-fragments android-studio

我的片段非常新鲜,这是我第一次尝试。这是我遇到的,我不知道如何解决,请帮帮我。这是我的left_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">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="Button"/>
</LinearLayout>

这是我的right_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"
    android:background="#00ff00">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:textSize="20sp"
        android:text="This is right fragment"/>
</LinearLayout>

他们预览的是,左: enter image description here

右:

enter image description here

这是LeftFragment.class

public class LeftFragment extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.left_fragment,container,false);
        return view;
    }
}

这是RightFrament.class

public class RightFrament extends Fragment{
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.right_fragment,container,false);
        return view;
    }
}

这是布局文件activity_main.xml

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

    <fragment
        android:id="@+id/left_fragment"
        android:layout_width="0dp"
        android:name="com.example.gaby.fragmenttest.LeftFragment"
        android:layout_weight="1"
        android:layout_height="match_parent" />

    <fragment
        android:id="@+id/right_fragment"
        android:name="com.example.gaby.fragmenttest.RightFrament"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1" />

</LinearLayout>

此布局文件的预览如下: enter image description here

我觉得很奇怪,我在Genymotion nexus7中打开它,看起来是正确的。这意味着android studio布局预览出错了,如何解决? enter image description here

1 个答案:

答案 0 :(得分:1)

activity_main.xml中使用tools:layout="..."属性。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    ...
    >

    <fragment
        android:id="@+id/left_fragment"
        ....
        tools:layout="@layout/left_fragment"
        />

    <fragment
        android:id="@+id/right_fragment"
        ...
        tools:layout="@layout/right_fragment"
        />

</LinearLayout>