【问题标题】:LayOut Inflater in android?Android中的LayOut Inflater?
【发布时间】:2010-10-05 05:20:09
【问题描述】:

我在Activity 中创建了View 的子类作为内部类。我只想将 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>

【问题讨论】:

  • 对我来说还不够清楚。你能分享你的部分代码吗?
  • @Cristian:我已经粘贴了代码。现在可以看看吗?
  • @Cristian: 我不知道如何在扩展 View 的类中添加 UI 组件
  • 您需要自己的View“扩​​展”版本的原因是什么?
  • @Asahi:我想在我的应用程序中使用 onDraw()....等

标签: android android-layout layout-inflater


【解决方案1】:

请记住,一旦你膨胀了一些东西,它就只是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
       }
  }
}

【讨论】:

  • @Cristian:我试过这段代码。但它没有在 XML 文件中显示任何小部件。我需要对加载的 XML 中的 ImageView 和 Button 小部件执行操作。
  • @Cristian: 是否可以将 xml 加载到视图中并对对象进行操作。
  • 将您的视图(对象、TextView、按钮等)放在 your_layout.xml 中,然后通过调用view.findViewById(R.id.my_text_view) 访问它们
  • @Asahi:我试过这段代码。但它也没有在 XML 文件中显示任何小部件。我应该对 xml 进行任何修改吗...??
  • schemas.android.com/apk/res/android" xmlns:app="schemas.android.com/apk/res/com.drawing" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content">
猜你喜欢
  • 2014-09-06
  • 2019-02-23
  • 1970-01-01
  • 2016-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多