How to develop advanced LinearLayout from XML

Can anyone suggest a way to improve this API8 example? Although they say that opinions could be defined in XML, what they actually did was their code in java. I understand why they want it. They added some members to the extended LinearLayout, and the values ​​are determined at runtime.

According to about, everyone in the universe of layout directives should go to XML. But for this application, it makes sense to continue to set the text as it is in the runtime logic. So, we have a hybrid approach. Inflate views, then fill in dynamic text. It’s hard for me to figure out how to do this. Here is the source and what I tried.

from API8 Examples, List4.java

  private class SpeechView extends LinearLayout {
     public SpeechView(Context context, String title, String words) {
        super(context);

        this.setOrientation(VERTICAL);

        // Here we build the child views in code. They could also have
        // been specified in an XML file.

        mTitle = new TextView(context);
        mTitle.setText(title);
        ...

, LinearLayout : id = "@+ id/LinearLayout01", OnCreate

SpeechView sv = (SpeechView) findViewById(R.id.LinearLayout01);

, :

    public class SpeechView extends LinearLayout {
       public SpeechView(Context context) {
          super(context);
          System.out.println("Instantiated SpeechView(Context context)");
       }
       ...
+5
2

, , main_row.xml. ? . TextView , main.xml.

, . LinearLayout XML onCreate

setContentView(R.layout.main);

TextView XML View. .

LayoutInflater li = LayoutInflater.from(context);
LinearLayout ll = (LinearLayout) li.inflate(R.layout.main, this);
TextView mTitle = (TextView) ll.findViewById(R.id.roleHeading);

R.id.roleHeading - TextView, .

<TextView android:id="@+id/roleHeading" ... />

, LayoutInflater Activity, .

+3

. , () , , :

public class SpeechView extends LinearLayout {
        public SpeechView(Context context) {
           super(context);
           View.inflate(context, R.layout.main_row, this);
        }
        ...

, .

: .

+10

All Articles