我正在尝试将滚动视图放置在主要的线性布局中,并且在滚动视图内部是一个包含自定义视图的线性布局,我不太确定如何使它起作用。我是android studio的新手,如果您指出我的错误,将不胜感激。谢谢!我的自定义视图类中的代码如下所示:>
package com.example.myapplication; import android.content.Context; import android.content.res.Resources; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class mview extends View { @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } public mview(Context context) { super(context); init(null, 0); } public mview(Context context, AttributeSet attr) { super(context, attr); init(attr, 0); } public mview(Context context, AttributeSet attr, int def) { super(context, attr, def); init(attr, def); } private void init(AttributeSet set, int def) { line1.setColor(Color.BLACK); line1.setAntiAlias(true); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //drawing }
和我的mainactivity类中的代码看起来像这样
package com.example.myapplication; import android.widget.LinearLayout; import android.widget.ScrollView; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private void clicked() { mview.addLine(); } private mview mview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mview = findViewById(R.id.mview); setContentView(R.layout.activity_main); ScrollView scrollView = findViewById(R.id.scrollview); Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { clicked(); } }); } }
而且我的activity_main布局看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.myapplication.mview
android:id="@+id/mview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
我正在尝试将滚动视图放置在主要的线性布局中,并且在滚动视图内部是一个包含自定义视图的线性布局,我不太确定如何使它起作用。 ...
不用担心。如下所述,这只是一个小错误,当您考虑Layout_weight = 1时,高度将与全屏高度匹配,因此请从代码中删除该属性: