LayOut Inflater в Android? - PullRequest
       3

LayOut Inflater в Android?

1 голос
/ 05 октября 2010

Я создал подкласс View как внутренний класс в моем Activity. Я просто хочу накачать xml в класс, который расширяет View. Кто-нибудь может предоставить какой-нибудь код / ​​синтаксис как класса, так и xml?

public class DrawAct extends Activity {

       DrawView drawView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        drawView = new DrawView(this, null);
         setContentView(drawView);


    }
    public class DrawView extends View implements OnTouchListener {
        Path path;
        Paint paint = new Paint();
        private ShapeDrawable mDrawable;
        private ArrayList<Path> graphics = new 
        public DrawView(Context context,AttributeSet attrs) {
            super(context,attrs);
            //I want to load the contents of an xml in this class
    //
  }

}

xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res/com.drawing"
        android:orientation="vertical"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">

 <Button android:id="@+id/Button02" 
    android:layout_width="wrap_content" 
    android:layout_height="200dip"

     android:layout_gravity="right"></Button>
</LinearLayout>

1 Ответ

1 голос
/ 05 октября 2010

Просто имейте в виду, что как только вы что-то раздули, это будет просто View объект.Не уверен, что вы хотите сделать, но можете сделать что-то вроде:

public class drawiact extends Activity {
    DrawView drawView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        drawView = new DrawView(this, null);
        setContentView(drawView);
    }
    public class DrawView extends View implements OnTouchListener {
        Path path;
        Paint paint = new Paint();
        private ShapeDrawable mDrawable;
        private ArrayList graphics = new 
        public DrawView(Context context,AttributeSet attrs) {
            super(context,attrs);
            View view = getLayoutInflater().inflate(R.layout.your_layout, null);
            // then do whatever you want with the 'view' object
       }
  }
}
...