【问题标题】:Custom View not showing: no height, but still width自定义视图未显示:没有高度,但仍然是宽度
【发布时间】:2014-05-09 23:40:26
【问题描述】:

在我当前的项目中,我遇到了死胡同。我的自定义视图,仅创建以显示特定颜色,不会显示在我的模拟器视图中(尽管它似乎在 Eclipse 的编辑器中正确显示)除非它具有最小高度,但它只会得到这个准确的高度。我要match_parent

问题可能出在列表视图的项目布局中。

这是我的自定义视图的代码:

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class ColorView extends View {
    private int mColor;
    private Context mContext;
    private int width;
    private int height;

    public ColorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.ColorView, 0, 0);

        try {
            mColor = a.getColor(R.styleable.ColorView_color, 0xFFFFFF);
        } finally {
            a.recycle();
        }
    }

    public void setColor(int hexColor) {
        mColor = hexColor;
        invalidate();
    }

    public void setColorFromResource(int resID) {
        mColor = mContext.getResources().getColor(resID);
        invalidate();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.d("DEBUG", Integer.toString(w) + " " + Integer.toString(h));
        width = w;
        height = h;
    }

    @Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(mColor);
        Log.d("DEBUG", "drawn");
    }
}

还有我的布局文件的简化版本:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:raabeapp="http://schemas.android.com/apk/res/com.example.myapp"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/row_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

<com.example.myapp.ColorView
    android:id="@+id/status_view"
    android:layout_width="20dip"
    android:layout_height="match_parent"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:layout_marginRight="5dip"
    android:minHeight="50dp"
    raabeapp:color="#FF0000" />

<TextView
    android:id="@+id/hour_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/status_view"
    android:text="1"
    android:textSize="@dimen/hour_textsize"
    android:width="70dp" />

<TextView
    android:id="@+id/text_hourtext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_toRightOf="@+id/hour_view"
    android:text="@string/hour_text"
    android:textSize="@dimen/hourtext_textsize" />

【问题讨论】:

    标签: java android android-layout android-custom-view custom-view


    【解决方案1】:

    好的,我想出了一个解决方法:
    当我使用 LinearLayout 而不是 RelativeLayout 时,我得到了我想要的结果。
    看来问题出在RelativeLayout。如果有人知道这是为什么,或者我仍然可以使用RelativeLayout,我会非常感兴趣。这个答案没有提供完整的解决方案,但如果有人遇到我的问题,它可能会提供一个丑陋的解决方法。

    【讨论】:

      【解决方案2】:

      android:layout_height="match_parent" 用作您的RelativeLayout

      【讨论】:

      • 这没有提供问题的答案。要批评或要求作者澄清,请在其帖子下方发表评论。
      • @RobWatts 它确实回答了这个问题。它可能正确,也可能不正确(不打算测试),但在 Android 世界中,这是值得尝试的。
      • 对不起,我的回答迟了,我刚看到你的 cmets。
      • 好主意,但没有奏效。问题不在于行布局不适合行 - 我的组件有时比我的视图在最大尺寸时更高 - 而是视图根本没有高度,尽管它应该是。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-10
      • 1970-01-01
      • 2019-09-18
      • 2017-01-20
      相关资源
      最近更新 更多