Creo i fragments: una classe Java e un file XML.
Frammento.java:
public class Frammento extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v=inflater.inflate(R.layout.frammento,container,false);
return v;
}
}
frammento.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#AADDFF">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="@+id/button" />
</RelativeLayout>
Frammento2.java:
public class Frammento2 extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v=inflater.inflate(R.layout.frammento2,container, false);
return v;
}
}
frammento2.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="#FFFFAA">
<TextView
android:text="Questo è il frammento 2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView" />
</LinearLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
<fragment android:name="com.antonello.laboratorionuovo2.Frammento"
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="@+id/frammento"
android:layout_weight="1"/>
<fragment android:name="com.antonello.laboratorionuovo2.Frammento2"
android:layout_height="match_parent"
android:layout_width="0dp"
android:id="@+id/frammento2"
android:layout_weight="1"/>
</LinearLayout>
E così funziona perfettamente.
Nessun commento:
Posta un commento