如何在Android布局上绘制一些东西?

时间:2016-02-28 12:17:21

标签: android android-layout android-view

这是活动

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

我知道我可以创建一个类

public class DrawView extends View {
    Paint paint = new Paint();

    public DrawView(Context context) {
        super(context);
        paint.setColor(Color.BLACK);
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, 20, 20, paint);
        canvas.drawLine(20, 0, 0, 20, paint);
    }
}

并在活动中使用它

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

}

但它必须替换我原来的布局setContentView(R.layout.activity_main);

如果我使用布局,我无法绘制一些东西,如果我画了一些东西,我就不能使用布局

我怎样才能保留两者?

2 个答案:

答案 0 :(得分:2)

好吧,为此,您可以将View添加到XML布局中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center">

    <insert.your.real.package.DrawView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_gravity="center"/>

</LinearLayout>

目前,您拥有的内容不支持自定义XML属性。如果要添加XML属性,则需要添加更多构造函数。更详细的例子如下所示

DrawView类

public class DrawView extends View {

    public DrawView(Context context) {
        this(context, null);
    }

    public DrawView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public DrawView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);

        //you can also init your attributes here (if you have any)
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, 20, 20, paint);
        canvas.drawLine(20, 0, 0, 20, paint);
    }
}

有关使用自定义视图的详细信息,请务必阅读http://developer.android.com/training/custom-views/index.html。希望这会有所帮助:)

答案 1 :(得分:0)

好的,所以布局是&#34;添加&#34;视图,包含不同视图(如TextView,EditText等...)或ViewGroup(如LinearLayout,RelativeLayout等...)的文件(在您的res / layout中)。您必须对您将用于表示活动的布局说明,使用setContentView(布局)来执行此操作。所以你需要做的就是把你的res / layout中的一个文件放在setContentView(R.layout.mylayout)中,mylayout这里是你文件的名字。然后在mylayout.xml文件中,您必须添加自定义视图,这里是您的DrawView。例:

private void Start()
{
    m_FireButton = "Fire" + m_PlayerNumber;

    m_ChargeSpeed = (m_MaxLaunchForce - m_MinLaunchForce) / m_MaxChargeTime;
}

void OnTriggerStay(Collider other) {
    if (other.tag == "Player")  {
        Fire ();
        //This is where I want to add a delay!
    }
}

因此,您需要在activity_main布局中添加自定义视图。

您也可以将自定义视图直接放在setContentView中,但如果您希望它包含子视图,它应该从ViewGroup扩展。