【问题标题】:Custom view in the second row of a LinearLayout not getting displayedLinearLayout 的第二行中的自定义视图未显示
【发布时间】:2015-09-30 03:20:41
【问题描述】:
/* Code snipet from the MainActivity */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /* Linear layout  */
    LinearLayout layout = (LinearLayout)findViewById(R.id.main_container);

    /* Creating two similar custom views */
    CustomView row1 = new CustomView(this);  /* First row */
    CustomView row2 = new CustomView(this);  /* second row */

    layout.add(row1,0);
    layout.add(row2,1);
}

CustomView.java

public class CustomView extends View {

    public final String TAG = "CustomView";
    Context context;

    private Drawable backgroundDrawable;

    Rect backGroundDrawableRect;

    public TextMessageView(Context context) {
        super(context);
        this.context = context;
        backgroundDrawable = ContextCompat.getDrawable(context,R.drawable.background_drawable);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        super.onLayout(changed, left, top, right, bottom);

        backGroundDrawableRect.left = left;
        backGroundDrawableRect.right = right;
        backGroundDrawableRect.bottom = bottom;
        backGroundDrawableRect.top = top;

        backgroundDrawable.setBounds(backGroundDrawableRect);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /* 100 pixel height for the view */
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),100);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);

        if(backgroundDrawable != null) {
            backgroundDrawable.draw(canvas);
        }
    }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

</LinearLayout>

问题是在线性布局的第二行中添加的视图没有显示出来。在调试过程中,当我在开发人员选项中启用“显示布局绑定”时,我发现第二行中的视图占用了所需的空间,但没有显示可绘制的内容。

【问题讨论】:

    标签: android android-linearlayout android-custom-view


    【解决方案1】:

    自定义视图的变化

       public class CustomView extends View {
    
       public final String TAG = "CustomView";
       Context context;
    
       private Drawable backgroundDrawable;
    
       Rect backGroundDrawableRect;
    
        public CustomView(Context context) {
        super(context);
        this.context = context;
        backgroundDrawable = ContextCompat.getDrawable(context,R.drawable.ic_launcher);
        }
    
       @Override
       protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    
        super.onLayout(changed, left, top, right, bottom);
    
     /* backGroundDrawableRect.left = left;
        backGroundDrawableRect.right = right;
        backGroundDrawableRect.bottom = bottom;
        backGroundDrawableRect.top = top;
    
        backgroundDrawable.setBounds(backGroundDrawableRect);
     */
        }
    
      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        /* 100 pixel height for the view */
        setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),100);
      }
    
      @Override
      protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    
        if(backgroundDrawable != null) {
            backgroundDrawable.draw(canvas);
        }
       }
    

    您的启动器活动会进行此更改

      public class Test extends Activity {
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test1);
        /* Linear layout */
        LinearLayout layout = (LinearLayout) findViewById(R.id.main_container);
    
        /* Creating two similar custom views */
        CustomView row1 = new CustomView(getApplicationContext()); /* First row    */
        row1.setBackgroundColor(Color.RED);
        CustomView row2 = new CustomView(getApplicationContext()); /* Second row  */
    
        row2.setBackgroundColor(Color.GREEN);                           
    
        layout.addView(row1);
        layout.addView(row2);
      }
     }
    

    【讨论】:

    • 我已经在xml文件中指定了垂直方向
    • 检查 ans 这种方式也可以添加输出
    • 实际上,您并没有让自定义视图进行绘图,而是使用视图的方法来绘制我不想要的背景。
    【解决方案2】:

    Canvas 坐标会自动调整到视图的位置,因此您需要将 Drawable 的左边界和上边界设置为零:

    backGroundDrawableRect.left = 0;
    backGroundDrawableRect.right = right - left;
    backGroundDrawableRect.bottom = bottom - top;
    backGroundDrawableRect.top = 0;
    

    【讨论】:

    • 这是我的理解,任何需要在画布内绘制的东西,坐标都应该是相对于画布坐标的。对吗?
    • 是的,它是正确的:系统在调用 onDraw() 之前调用 canvas.translate() 以便坐标 0,0 与视图的左上角匹配。但请注意不要使用 canvas.getWidth() 和 canvas.getHeight(),因为画布可能比视图大。
    猜你喜欢
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    • 1970-01-01
    • 1970-01-01
    • 2020-01-20
    相关资源
    最近更新 更多