小编Nih*_*uri的帖子

从活动中膨胀片段

这是我的活动课。我正在尝试将片段膨胀到此活动。

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.fg1, new ButtonsFragment());
        ft.commit();
    }
Run Code Online (Sandbox Code Playgroud)

活动_main.xml

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/a_layout"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_margin="10dp"
        android:id="@+id/fg1"
        />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

ButtonsFragment.java

public class ButtonsFragment extends Fragment {

    Button b1,b2,b3;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
    {
        super.onCreateView(inflater,container,savedInstanceState);

       View v = inflater.inflate(R.layout.fragment_buttons,container,false);

        b2 = (Button)v.findViewById(R.id.b2);
        b1 = (Button)v.findViewById(R.id.b1);
        b3 = (Button)v.findViewById(R.id.b3);
        return v;
    }
}
Run Code Online (Sandbox Code Playgroud)

fragment_buttons.xml

<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="match_parent" …
Run Code Online (Sandbox Code Playgroud)

android android-fragments

3
推荐指数
1
解决办法
8551
查看次数

标签 统计

android ×1

android-fragments ×1