Creare due fragments in xml.
Cliccando direttamente su New -> Fragment ottengo sia il file xml sia quello java.
<?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="match_parent"
android:background="#AAFFFF"
tools:context=".primo">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/testo1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
(ci ho aggiunto uno sfondo celeste per distinguerlo)
public class primo extends Fragment {
public primo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_primo, container, false);
}
}
Adesso ottengo l'altro frammento:
Ecco, ho creato un frammento lasciando il nome di default, BlankFragment.
Vediamone il file xml e il file java.
<?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="match_parent"
android:background="#FFAAAA"
tools:context=".BlankFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
Ci ho aggiunto uno sfondo rosso.Ora devo riportare questi fragment sulla MainActivity: ho creato un altro video:
Ed ecco:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="@+id/container"
tools:context=".MainActivity">
<fragment
android:id="@+id/fragment"
android:name="com.example.antonello.studiofragment.primo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<fragment
android:id="@+id/fragment2"
android:name="com.example.antonello.studiofragment.BlankFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
I due fragment hanno l'altezza impostata a wrap_content, per cui appaiono della stessa altezza delle textview compresevi dentro:
Ora vediamo il file java.
public class primo extends Fragment {
public primo() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_primo, container, false);
}
}
Ecco, praticamente viene inflatato il codice xml del fragment all'interno del ViewGroup chiamato container.Tutto qui.
Nessun commento:
Posta un commento