Anfängerfrage zu Views

  • 1 Antworten
  • Letztes Antwortdatum
N

Nanika

Neues Mitglied
0
Hallo zusammen,

ich habe folgenden Code:
Code:
public class MainActivity extends Activity {

    public static final String EXTRA_MESSAGE_DEF = "com.example.comm.MESSAGE";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.mainLayout, new PlaceholderFragment())
                    .commit();
        }
    }
...
Dazu einmal fragment_main.xml:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainRelativeLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#0000ff"
    tools:context="com.example.comm.MainActivity$PlaceholderFragment" >    
     <Button android:id="@+id/button_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:onClick="click"
            android:text="@string/button_text"/>
     <EditText android:id="@+id/edit_text_main"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_centerHorizontal="true"
              android:layout_below="@id/button_main" 
              android:hint="@string/editTextHint">
    </EditText>
   
</RelativeLayout>
Und activity_main.xml:
Code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mainLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.comm.MainActivity"
    tools:ignore="MergeRootFrame" >    
</RelativeLayout>
Was ich nicht verstehe: ich sage ja
Code:
setContentView(R.layout.activity_main);
und bekomme mein Layout angezeigt. Aber in activity_main.xml ist doch gar kein Layout definiert, sondern in fragment_main.xml, das hier aber gar nicht angefasst wird. Warum ist das so?
Ich hoffe, es erbarmt sich jemand meiner. Die meisten Tutorials haben mir zu wenig Beispiele, und als nicht abstrakt denkender Mensch raffe ich es nicht ohne. :sad:

Danke im Voraus :)
 
Code:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.mainLayout, new PlaceholderFragment())
                    .commit();
        }
    }
In diesem Code setzt du mit setContentView dein activity_main.xml Layout. Dieses beinhaltet NUR das RealtiveLayout.

In der if holst du dir von der Activity den FragmentManager und setzt "auf" das RealtiveLayout von activity_main das PlaceholderFragment. Dieses hast du nicht gepostet, hat aber mitsicherheit so etwas in der getView():
Code:
View convert = inflater.inflate(R.layout.fragment_main.xml, null);
return convert;

Deshalb siehst du das fragment_main-Layout.

Gruß
 
Zurück
Oben Unten