首页 > 其他 > 详细

设置行间距

时间:2014-03-31 10:30:40      阅读:841      评论:0      收藏:0      [点我收藏+]

设置行间距有四种方法分别是:

(1)在布局文件中使用android:lineSpacingExtra 设置精准的行间距 使用android:lineSpacingMultiplier属性设置默认行间距的倍数

(2)使用Style资源设置行间距,如果有多个控件需要设置行间距,使用Style会非常方便,也容易维护

(3)使用setLineSpacing方法设置行间距

(4)在代码中设置行间距

先看main方法中的代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.luohaibo.testdemo03;
 
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
 
public class MainActivity extends Activity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         
        TextView textView = (TextView) findViewById(R.id.textview);
        textView.setLineSpacing(50, 1.2f);
    }
 
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

 布局文件的代码如下

bubuko.com,布布扣
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="fill_parent"
 4     android:layout_height="fill_parent">
 5 
 6 
 7     <TextView android:layout_width="match_parent"
 8         android:layout_height="wrap_content"
 9         android:background="#FFF"
10         android:textColor="#000"
11         android:text="第一行的文本\n第二行的文本(行间距为20dp)"
12         android:lineSpacingExtra="20dp" android:layout_margin="10dp"/>
13     <TextView android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:background="#FFF"
16         android:textColor="#000"
17         android:text="第一行的文本\n第二行的文本(行间距是默认行间距的1.8倍)"
18         android:lineSpacingMultiplier="1.8" android:layout_margin="10dp"/>
19     <TextView android:layout_width="match_parent"
20         android:layout_height="wrap_content"
21         android:background="#FFF"
22         android:textColor="#000"
23         android:text="第一行的文本\n第二行的文本(行间距是默认行间距的1.5倍)"
24         style="@style/line_space"
25          android:layout_margin="10dp"/>
26     <TextView android:layout_width="match_parent"
27         android:id="@+id/textview"
28         android:layout_height="wrap_content"
29         android:background="#FFF"
30         android:textColor="#000"
31         android:text="第一行的文本\n第二行的文本(用代码设置行间距)"
32         android:layout_margin="10dp"/>
33     
34 </LinearLayout>
bubuko.com,布布扣

 

 setLineSpacing方法有两个参数,都是float类型,第一个参数相当于android:lineSpacingExtra属性,第二个参数相当于android:linSpacingMultiplier属性。至于系统会采用哪个参数作为最终的行间距,要看哪个参数值所表示的行间距大了

设置行间距,布布扣,bubuko.com

设置行间距

原文:http://www.cnblogs.com/bukebujie/p/3634514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!