资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

android换行,安卓自动换行

Android 实现 EditText 文本自动换行

很简单,在布局 XML 文件的 EditText 中加上下面这行:

在龙州等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供成都网站设计、网站制作 网站设计制作定制网站,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销推广,成都外贸网站制作,龙州网站建设费用合理。

android:inputType="textMultiLine"

即可。

android 中组件怎么换行

应用中获取会用到需要自动换行的控件,而这并不是一般的线性或者相对布局就能实现的,在此分享下自定义控件。原型是在网上找到的,在此稍作了修改。

这是设计出的样稿,样稿中的较高的图片是从一个数据集中的穿插在另一个数据集中的,Textview的长度需要根据文字的长度不同而设置,而左右需要平分,做法如下:

1.将总体分为两个数据集:左右,并用2个LinearLayout分别装自定义控件

android:layout_height="wrap_content"

android:orientation="horizontal"

android:layout_marginLeft="5dip"

android:layout_marginRight="5dip"

android:layout_marginTop="5dip"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/span style="line-height: 21px;"PredicateLayout

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:layout_weight="1"

android:orientation="vertical"

span style="line-height: 21px;"PredicateLayout android:id="@+id/righttab"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

/span style="line-height: 21px;"PredicateLayout

2.自定义控件

public class PredicateLayout extends LinearLayout {

int mLeft, mRight, mTop, mBottom;

Hashtable map = new Hashtable();

public PredicateLayout(Context context) {

super(context);

}

public PredicateLayout(Context context, int horizontalSpacing, int verticalSpacing) {

super(context);

}

public PredicateLayout(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int mWidth = MeasureSpec.getSize(widthMeasureSpec);

int mCount = getChildCount();

int mX = 0;

int mY = 0;

mLeft = 0;

mRight = 0;

mTop = 5;

mBottom = 0;

int j = 0;

View lastview = null;

for (int i = 0; i mCount; i++) {

final View child = getChildAt(i);

child.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

// 此处增加onlayout中的换行判断,用于计算所需的高度

int childw = child.getMeasuredWidth();

int childh = child.getMeasuredHeight();

mX += childw; //将每次子控件宽度进行统计叠加,如果大于设定的高度则需要换行,高度即Top坐标也需重新设置

Position position = new Position();

mLeft = getPosition(i - j, i);

mRight = mLeft + child.getMeasuredWidth();

if (mX = mWidth) {

mX = childw;

mY += childh;

j = i;

mLeft = 0;

mRight = mLeft + child.getMeasuredWidth();

mTop = mY + 5;

//PS:如果发现高度还是有问题就得自己再细调了

}

mBottom = mTop + child.getMeasuredHeight();

mY = mTop; //每次的高度必须记录 否则控件会叠加到一起

position.left = mLeft;

position.top = mTop + 3;

position.right = mRight;

position.bottom = mBottom;

map.put(child, position);

}

setMeasuredDimension(mWidth, mBottom);

}

@Override

protected LayoutParams generateDefaultLayoutParams() {

return new LayoutParams(1, 1); // default of 1px spacing

}

@Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

// TODO Auto-generated method stub

int count = getChildCount();

for (int i = 0; i count; i++) {

View child = getChildAt(i);

Position pos = map.get(child);

if (pos != null) {

child.layout(pos.left, pos.top, pos.right, pos.bottom);

} else {

Log.i("MyLayout", "error");

}

}

}

private class Position {

int left, top, right, bottom;

}

public int getPosition(int IndexInRow, int childIndex) {

if (IndexInRow 0) {

return getPosition(IndexInRow - 1, childIndex - 1)

+ getChildAt(childIndex - 1).getMeasuredWidth() + 8;

}

return getPaddingLeft();

}

}

3.将数据分别填充到左右两个控件中

这应该算是自动换行经典实例了吧,相信这个搞定以后同类型的需求都不成问题了。

android textview 怎么换行?

textView如果想要强制换行的话,必须先把TextView显示方式修改为多行(android:singleLine="false"),然后才能换行。

方法一般用两种:

1、在字符串里加入“\n”,如"abc\nrc";

2、把TextView设置为固定宽度,然后让系统自动换行。如android:layout_width="100dp";

扩展资料

Class Overview

向用户显示文本,并可选择允许他们编辑文本。TextView是一个完整的文本编辑器,但是基类为不允许编辑;其子类EditText允许文本编辑。

允许用户复制部分或全部内容,将其粘贴到别的地方,设置XML属性Android:textisselectable :“真” 或设置相关方法 settextisselectable 为“真”。textisselectable flag 允许用户在TextView选择手势,从而触发系统内置的复制/粘贴控件。

Displays text to the user and optionally allows them to edit it. A TextView is a complete text editor, however the basic class is configured to not allow editing; seeEditTextfor a subclass that configures the text view for editing.

To allow users to copy some or all of the TextView's value and paste it somewhere else, set the XML attributeandroid:textIsSelectableto "true" or callsetTextIsSelectable(true). ThetextIsSelectableflag allows users to make selection gestures in the TextView, which in turn triggers the system's built-in copy/paste controls.

参考资料来源:百度百科:TextView


新闻标题:android换行,安卓自动换行
转载来源:http://cdkjz.cn/article/dssgoce.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220