Aufblasen TextView LinearLayout und programmgesteuert

Hier das problem ich bekomme nicht den gleichen Ausgang "Ergebnis anzeigen" in diesen beiden Fällen möchte ich beheben, Fall 1 um denselben output Ergebnis Fall 2:

  1. Aufblasen textview linearlayout und, und fügen Sie dann textview linearlayout zu programmatisch.
  2. Hinzufügen textview linearlayout zu in xml.

Code und die Ausgabe Für Fall 1:

main.xml

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

text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:textColor="#000000"
    android:text="1"
    android:textSize="20sp"
    android:background="#AAAAAA"
    android:gravity="center"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_marginTop="2dp"
    android:layout_marginRight="2dp"
 />

onCreate-Methode in LayoutTestActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LinearLayout lt = (LinearLayout) getLayoutInflater().inflate(R.layout.main, null);
    TextView tv = (TextView) getLayoutInflater().inflate(R.layout.text_view, null);
    lt.addView(tv);
    setContentView(lt);
}

Ausgabe anzeigen (Nicht Korrekt)

Aufblasen TextView LinearLayout und programmgesteuert

- Code und Output-Fall 2:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    <TextView
        android:textColor="#000000"
        android:text="1"
        android:textSize="20sp"
        android:background="#AAAAAA"
        android:gravity="center"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_marginTop="2dp"
        android:layout_marginRight="2dp"
     />
</LinearLayout>

onCreate-Methode in LayoutTestActivity.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
}

Ausgabe-Ansicht (Korrektur)

Aufblasen TextView LinearLayout und programmgesteuert

InformationsquelleAutor thalsharif | 2011-10-26
Schreibe einen Kommentar