如何在Android布局上绘制一些东西?
这就是活动如何在Android布局上绘制一些东西?,android,android-layout,android-view,Android,Android Layout,Android View,这就是活动 public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FloatingActionBut
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\u main)代码>
如果我使用布局,我就不能画东西,如果我画东西,我就不能使用布局
如何同时保留这两个视图?好的,所以布局是视图的“添加”,一个包含不同视图(如文本视图、编辑文本等)或视图组(如线性布局、相对布局等)的文件(在您的res/layout中)。您必须对您的活动说,您将使用哪种布局来表示您的活动,您可以使用setContentView(布局)来完成它。因此,您必须做的事情是将一个文件放在res/layout中,就像setContentView(R.layout.mylayout)一样,这里的mylayout是您的文件名。然后在mylayout.xml文件中,您必须添加自定义视图,这是您的DrawView。例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<mypackagename.DrawView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
因此,在这里,您需要做的是在activity_主布局中添加自定义视图
您也可以将自定义视图直接放在setContentView中,但如果您希望它包含子视图,它应该从ViewGroup中扩展。好的,因此布局是视图的“添加”,一个包含不同视图(如TextView、EditText等)或视图组(如LinearLayout、RelativeLayout等)的文件(在您的res/layout中)。您必须对您的活动说,您将使用哪种布局来表示您的活动,您可以使用setContentView(布局)来完成它。因此,您必须做的事情是将一个文件放在res/layout中,就像setContentView(R.layout.mylayout)一样,这里的mylayout是您的文件名。然后在mylayout.xml文件中,您必须添加自定义视图,这是您的DrawView。例如:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<mypackagename.DrawView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
因此,在这里,您需要做的是在activity_主布局中添加自定义视图
您也可以将自定义视图直接放在setContentView中,但如果您想让它包含子视图,它应该从ViewGroup扩展而来。要做到这一点,您可以像这样将视图添加到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 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>
有关使用自定义视图的详细信息,请参阅。希望这有帮助:)这是行不通的。如果我删除第3-4个构造函数,并更改这个(上下文,attrs,0)代码>到super(上下文,属性,0)代码>,则它将工作。它不工作。如果我删除第3-4个构造函数,并更改这个(上下文,attrs,0)代码>到super(上下文,属性,0)代码>,那么它就可以工作了