Android 在编辑文本上绘制多行
我想创建一个记事本。我指的是android sdk提供的示例记事本。我的问题是我没能在editText上画多条线。我成功地运行了这个程序,但是编辑文本上没有显示任何内容,更糟糕的是,我无法在上面输入一个单词。这是我的密码。谢谢你的帮助 在视图中,我对我的类路径进行了双重确认。因此,我认为我的类路径没有任何问题Android 在编辑文本上绘制多行,android,Android,我想创建一个记事本。我指的是android sdk提供的示例记事本。我的问题是我没能在editText上画多条线。我成功地运行了这个程序,但是编辑文本上没有显示任何内容,更糟糕的是,我无法在上面输入一个单词。这是我的密码。谢谢你的帮助 在视图中,我对我的类路径进行了双重确认。因此,我认为我的类路径没有任何问题 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/layouteditbottom"
android:layout_alignParentBottom="true"
android:paddingBottom="18dp"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_above="@+id/layouteditbottom">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/title"
android:id="@+id/title_text1" />
<EditText android:id="@+id/title"
android:layout_toRightOf="@+id/title_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"
android:textSize="18sp"/>
<View xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.apps2.MainActivity$LineEditText"
android:id="@+id/body"
android:layout_below="@+id/title"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
android:padding="5dp"
android:scrollbars="vertical"
android:fadingEdge="vertical"
android:gravity="top"
android:textSize="22sp"
android:capitalize="sentences"/>
</RelativeLayout>
</RelativeLayout>
下面的代码是java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public static class LineEditText extends EditText{
// we need this constructor for LayoutInflater
public LineEditText(Context context, AttributeSet attrs) {
super(context, attrs);
mRect = new Rect();
mPaint = new Paint();
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
mPaint.setColor(Color.BLUE);
}
private Rect mRect;
private Paint mPaint;
@Override
protected void onDraw(Canvas canvas) {
//int count = getLineCount();
int height = getHeight();
int line_height = getLineHeight();
int count = height / line_height;
if (getLineCount() > count)
count = getLineCount();//for long text with scrolling
Rect r = mRect;
Paint paint = mPaint;
int baseline = getLineBounds(0, r);//first line
for (int i = 0; i < count; i++) {
canvas.drawLine(r.left, baseline + 1, r.right, baseline + 1, paint);
baseline += getLineHeight();//next line
}
super.onDraw(canvas);
}
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
公共静态类LineEditText扩展了EditText{
//我们需要这个构造器来实现LayoutFlater
公共LineEditText(上下文、属性集属性){
超级(上下文,attrs);
mRect=新的Rect();
mPaint=新油漆();
mPaint.setStyle(绘制、样式、填充和笔划);
mPaint.setColor(Color.BLUE);
}
私人直肠;
私人油漆;
@凌驾
受保护的void onDraw(画布){
//int count=getLineCount();
int height=getHeight();
int line_height=getLineHeight();
int count=高度/线条高度;
如果(getLineCount()>count)
count=getLineCount();//用于带滚动的长文本
Rect r=mRect;
油漆油漆=mPaint;
int baseline=getLineBounds(0,r);//第一行
for(int i=0;i
使用android:lines=“2”
修复多行和修复单行使用android:singleLine=“true”
在XML中,您已经使用了单行,请将其删除并使用,如示例所示
更改编辑文本,如下所示
<EditText android:id="@+id/title"
android:layout_toRightOf="@+id/title_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
//android:singleLine="true"
android:lines="2"// fix the multiline limit 2,3,4,5,6,7.....
android:imeOptions="actionNext"
android:textSize="18sp"/>
只需在编辑文本中添加一个背景图像,上面有行 编辑 创建一个可绘制的位图,将tileMode设置为“重复”,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@[package:]drawable/drawable_resource"
android:tileMode="repeat" />
对于
android:src
指定一个图像,其中包含一行文字和一些与一行文字高度相匹配的空格(取决于字体大小)。感谢所有回答的人。我通过将视图从最外层的relativeLayout替换为内部的relativeLayout来解决这个问题。下面的代码就是答案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/layouteditbottom"
android:layout_alignParentBottom="true"
android:paddingBottom="18dp"
/>
<RelativeLayout
android:id="@+id/toplayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:layout_alignParentTop="true">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:text="@string/title"
android:id="@+id/title_text1" />
<EditText android:id="@+id/title"
android:layout_toRightOf="@+id/title_text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"
android:textSize="18sp"/>
<TextView
android:id="@+id/notelist_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingRight="10sp"
android:textSize="18sp" />
</RelativeLayout>
<view
xmlns:android="http://schemas.android.com/apk/res/android"
class="com.example.note1.NoteEdit$LineEditText"
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/layouteditbottom"
android:layout_below="@+id/toplayout"
android:background="@android:color/transparent"
android:capitalize="sentences"
android:fadingEdge="vertical"
android:gravity="top"
android:padding="5dp"
android:scrollbars="vertical"
android:textSize="22sp" />
</RelativeLayout>
您想做什么??要用多行划去文本还是要在编辑文本框中划去多行?我要在文本下面划线。我想创建一个以线条填充背景的记事本。如果您已经解决了问题,请在下面将您的答案标记为已接受。不要在问题的标题中添加“[solved]”之类的伪标记。我想创建一个背景,当我向下滚动它时,它会像一些简单的记事本一样移动。创建一个位图drawable用作背景,并在该drawable中设置`android:tileMode=“repeat”。请参阅更新的答案以获取示例。感谢您的回答,但同时我想为我的问题不够清楚表示歉意。我想在正文文本上创建一行。