【问题标题】:Extending View class and layouts in Android在 Android 中扩展 View 类和布局
【发布时间】:2012-12-09 12:07:06
【问题描述】:

我正在使用 Android 中的布局和视图。我的布局如下:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/LinearLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
        <com.mycode.MyView
            android:id="@+id/MyView"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        />

        <LinearLayout
            android:id="@+id/leftpane"
            android:layout_width="55dp"
            android:layout_height="wrap_content"
            android:layout_weight="0.95"
            android:background="@color/leftpane_background"
            android:gravity="center"
            android:orientation="vertical"
            android:scrollbarStyle="insideOverlay" >

        </LinearLayout>
</LinearLayout>

如果我注释&lt;com.mycode.MyView .... /&gt; 行,则显示左侧线性布局(id 为leftpane),否则不显示。如果我在左窗格布局之后添加另一个布局(例如滚动视图布局),它也不会显示。 为什么使用我的自定义视图 (MyView) 会使所有其他布局消失? 我想做的是为应用程序的背景设置我的自定义视图,我将在其中显示图像,以及背景上的其他一些视图,如滚动视图、按钮等。 这是 MyView 类的代码。请记住,我需要在活动的背景上绘制图像(在整个背景上,显然不包括操作栏):

/**
*/
class   MyView extends View
{
    TextPaint   text_paint;
    Paint       paint;

    private void    InitView()
    {
        text_paint = new TextPaint( Paint.ANTI_ALIAS_FLAG );
        text_paint.setColor( Color.BLACK );
        paint = new Paint();
    }

    public MyView(Context context)
    {
        super(context);
        InitView();
    }

    public MyView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        InitView();
    }


    public MyView(Context context, AttributeSet attrs, int defaultStyle)
    {   
        super(context, attrs, defaultStyle);
        InitView();
    }


    @Override
    protected void  onDraw( Canvas canvas )
    {
        super.onDraw(canvas);
        //int   w, h;
        //w = canvas.getWidth();
        //h = canvas.getHeight();
        //paint.setColor( Color.WHITE );
        //canvas.drawRect(0, 0, w-1, h-1, paint );
        //canvas.drawText( "ciao", 100, 100, text_paint);
    }

};

【问题讨论】:

    标签: android layout android-custom-view


    【解决方案1】:

    尝试将视图的高度更改为:

    android:layout_height="wrap_content" 
    

    【讨论】:

    • 真的是一个空实现:我只是调用了超类的onDraw()方法。没有别的了。
    • 尝试将您的视图更改为 EditText 或任何订单标准小部件。
    • 我改了。如果我使用 fill_parent,我会看到一个大的编辑文本视图,而没有其他任何内容。如果我使用 wrap_content,编辑文本的高度会更小,我可以在屏幕上看到其他布局。我需要一种方法在应用程序的背景上绘制图像(可以随时间变化),但仍然在背景上显示布局(按钮、滚动视图等)。
    • 如果你的view是用来显示activity的背景,那么我想你可以使用FrameLayout而不是LinearLayout。
    • @MarcoMasci 也许你应该使用 FrameLayout 或合并替换根 LinearLayout
    【解决方案2】:

    当您覆盖自定义 MyView 中的 onMeasure 方法时,您可能做了一些错误工作, 所以 MyView 匹配所有的父视图。 你能在 MyView 的 onLayout 和 onMeasure 方法中显示你的代码吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-04-10
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-07
      相关资源
      最近更新 更多