将新视图绘制到相对布局中

Posted

技术标签:

【中文标题】将新视图绘制到相对布局中【英文标题】:Drawing a new view into a relative layout 【发布时间】:2012-06-13 04:57:08 【问题描述】:

我们正在为汽车编写“仪表板”。计算/绘制车速表的代码是与其余代码分开完成的,现在我正在尝试将两者结合起来。

它们都单独工作,并且在速度计视图代码中,UI 的主要部分仍在运行,我只是看不到速度计。

我已经编译好了,没有明显的错误。但是,当应用程序运行时,我看不到速度计出现在主视图中,即使我已经使用 addView 方法添加了它。

供参考:

我创建和添加视图的代码:

    setContentView(R.layout.main); 
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.RelativeLayout1);
    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.CENTER_VERTICAL);
    myView v = new myView(this);
    v.setLayoutParams(lp);
    relativeLayout.addView(v, lp);

myView 类的代码: 导入android.content.Context; 导入android.graphics.Bitmap; 导入android.graphics.BitmapFactory; 导入android.graphics.Canvas; 导入android.graphics.Color; 导入android.graphics.Paint; 导入android.graphics.RectF; 导入android.graphics.Typeface; 导入android.view.View;

public class myView extends View 
    private Paint mPaints;
    private Paint textpaint;
    private boolean mUseCenters;
    private RectF mBigOval;
    private float mStart;
    private float mSweep;
    public float SPEED_raw;
    public int SPEED = 0;   //initialized to 0 just for loop.  Remove initialization when IF statement below is deleted
    public static Bitmap gaugefront;
    public static Bitmap gaugeback;

    public myView(Context context) 
        super(context);
        mPaints = new Paint();
        mPaints.setAntiAlias(true);
        mPaints.setColor(Color.YELLOW);

        textpaint = new Paint();
        textpaint.setAntiAlias(true);
        textpaint.setColor(Color.WHITE);
        textpaint.setTextSize(150);
        textpaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));

        gaugefront = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugefront);    //Load gaugefront.png
        gaugeback = BitmapFactory.decodeResource(context.getResources(),R.drawable.gaugeback);      //Load gaugeback.png

        mUseCenters = true;
        mBigOval = new RectF(400, 10, 880, 490);                //left[x-coordinate],top[y-coordinate],right[x],bottom[y]
    

    private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint) 
        canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
    

    @Override 
    protected void onDraw(Canvas canvas) 
        canvas.drawBitmap(myView.gaugeback, 400, 10, null);     //draw gauge background, X coordinate:400, Y coordinate: 10
        drawArcs(canvas, mBigOval, mUseCenters, mPaints);

        //SPEED_raw = 70;                   //Raw float data from CCS. Uncomment when the IF statement below is deleted.
        SPEED = Math.round(SPEED_raw);      //SPEED integer.  Units km/h - Top speed of 135. Used for digital readout

        mStart = 90;                        //Start drawing from -90 degrees (Counter clockwise)                
        mSweep = SPEED_raw*2;               //Draw stop point

            if(SPEED >= 135)           //JUST FOR SHOW.  Delete this for actual speedo
                SPEED_raw = 0;          // "
                                       // "
            else                        // "
                SPEED_raw += 0.5;       // "

        canvas.drawBitmap(myView.gaugefront, 400, 10, null);                //draw gauge foreground, X coordinate:400, Y coordinate: 10

        String speed_string = String.valueOf(SPEED);                        //Convert int SPEED to String for display

//            while (speed_string.endsWith(".0") || speed_string.endsWith("."))            //Erase trailing ".0" for float types. Don't need if SPEED is an int
//              speed_string = (speed_string.substring(0, speed_string.length() - 1));
//            

        canvas.drawText(speed_string, 640, 420, textpaint);                 //arguments: (string, x-coordinate, y-coordinate, paint)

        invalidate();
    

这是我的 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_
android:layout_
android:gravity="center"
android:keepScreenOn="true"
android:orientation="vertical" >

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_
    android:layout_ >

<ImageView
    android:id="@+id/solarbg"
    android:layout_
    android:layout_
    android:src="@drawable/solarbg" />
</LinearLayout>

<ImageView
    android:id="@+id/l_lamp"
    android:layout_
    android:layout_
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_gravity="left"
    android:src="@drawable/l_arrow_dim" />

<DigitalClock
    android:id="@+id/digitalClock1"
    android:layout_
    android:layout_
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:gravity="center"
    android:textColor="#ffffff"
    android:textSize="50dp" />

<ImageView
    android:id="@+id/r_lamp"
    android:layout_
    android:layout_
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:src="@drawable/r_arrow_dim" />

<TextView
    android:id="@+id/setAmps"
    android:layout_
    android:layout_
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:gravity="left"
    android:text="@string/setAmps"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/setAmpsVal"
    android:layout_
    android:layout_
    android:gravity="left"
    android:layout_below = "@+id/setAmps"
    android:layout_alignParentLeft="true"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/setVelocity"
    android:layout_
    android:layout_
    android:layout_alignBottom="@+id/setAmps"
    android:layout_alignParentRight="true"
    android:layout_centerVertical="true"
    android:gravity="right"
    android:text="@string/setVelocity"
    android:textColor="#ffffff"
    android:textSize="30dp" />

    <TextView
    android:id="@+id/setVelocityVal"
    android:layout_
    android:layout_
    android:gravity="right"
    android:layout_below = "@+id/setVelocity"
    android:layout_alignParentRight="true"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/actVelocity"
    android:layout_
    android:layout_
    android:layout_alignParentRight="true"
    android:layout_below="@+id/setVelocityVal"
    android:gravity="right"
    android:text="@string/actVelocity"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/actVelocityVal"
    android:layout_
    android:layout_
    android:layout_below = "@+id/actVelocity"
    android:layout_alignParentRight="true"
    android:gravity="right"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/busAmps"
    android:layout_
    android:layout_
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/setAmpsVal"
    android:gravity="left"
    android:text="@string/busAmps"
    android:textColor="#ffffff"
    android:textSize="30dp" /><TextView
    android:id="@+id/busAmpsVal"
    android:layout_
    android:layout_
    android:gravity="left"
    android:layout_below = "@+id/busAmps"
    android:maxEms = "5"
    android:textColor="#ffffff"
    android:textSize="30dp" />

  <TextView
    android:id="@+id/powerVal"
    android:layout_
    android:layout_
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom = "true"
    android:gravity="right"
    android:textColor="#ffffff"
    android:textSize="30dp" />

<TextView
    android:id="@+id/power"
    android:layout_
    android:layout_
    android:layout_alignParentRight="true"
    android:layout_above = "@+id/powerVal"
    android:gravity="left"
    android:text="@string/power"
    android:textColor="#ffffff"
    android:textSize="30dp" />


 <TableLayout
    android:id="@+id/msgCenterLayout"
    android:layout_
    android:layout_ 
    android:layout_alignParentBottom = "true" 
    android:layout_centerHorizontal = "true"
    android:orientation = "vertical"
    android:visibility = "invisible">

<ScrollView
    android:id="@+id/messageCenterText"
    android:layout_
    android:layout_
    android:gravity="left"
    android:text="@string/busAmps"
    android:textColor="#ffffff"
    android:textSize="30dp" />


</TableLayout>

</RelativeLayout>

编辑: 我正在使用一个 XML 文件作为主布局,并尝试将此新视图添加到该 XML 文件内的相对布局中。

【问题讨论】:

你能在这里粘贴你的 XML 布局代码吗?我已经编辑了我的答案 添加了 XML,我只是在我的平板电脑而不是模拟器上运行它。事实证明它确实有效,但我的对手使用像素而不是 dp 所以它不能很好地缩放......并且居中是一个问题。旋转后看起来不错,但我需要处理它 【参考方案1】:

试着写

LayoutParams p = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT); 

v.setLayoutParams(lp);
relativeLayout.addView(v);

【讨论】:

这样我在 RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);行编辑:没关系,只需要导入!谢谢。但是,现在我的车速表的左下角正排列在屏幕的中心......有没有办法将它偏移与平局相同的量,和/或调整它的大小以适应? 您遇到什么错误?您能否更具体地了解 myView 类。 抱歉,刚刚更新了我的评论,我只需要导入,现在它可以工作了 它并不完全有效,现在主屏幕上的绘图显示不正确,部分被绘制在尴尬的地方

以上是关于将新视图绘制到相对布局中的主要内容,如果未能解决你的问题,请参考以下文章

如何避免相对布局中可绘制背景的额外高度?

如何在相对布局中使用边框(可绘制)?

动态添加的视图在相对布局中重叠

了解自动布局中的乘数以使用相对定位

视图不显示在相对布局中

在相对布局下方的相对布局中显示按钮