gravity和layout_gravity的区别 android:gravity与android:layout_gravity。他们的区别在于:android:gravity用于设置View组件的对齐方式,而android:layout_gravity用于设置Container组件的对齐方式 线性布局是最常用的布局线性布局在xml文件中使用<LinearLayout>来定义 线性布局可以分为水平和垂直的方向的布局,可以通过android:orientation=“vertical”来定义方向,该属性可以有horizontal和vertical两个方向。(线性布局才有)

android:id —— 为控件指定相应的ID
android:text —— 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串
android:grivity —— 指定控件的基本位置,比如说居中,居右等位置(控件里面的内容位置)
android:textSize —— 指定控件当中字体的大小
android:background —— 指定该控件所使用的背景色,RGB命名法
android:width —— 指定控件的宽度
android:height —— 指定控件的高度
android:padding* —— 指定控件的内边距,也就是说控件当中的内容
android:layout_margin*——指定控件的外边距,也就是说控件跟父控件之间的距离
android:sigleLine —— 如果设置为真的话,则将控件的内容在同一行当中进行显示

控件的布局,可以再java代码中动态的设置,也可以在布局文件中设置(比较死板)

 1 import android.os.Bundle; 2 import android.app.Activity; 3  4 public class Layout01 extends Activity { 5     @Override 6     protected void onCreate(Bundle savedInstanceState) { 7         super.onCreate(savedInstanceState); 8         setContentView(R.layout.main); 9     }10 }

布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    >    <TextView        android:id="@+id/firstText"        android:text="第一行"        android:gravity="center_vertical"        android:textSize="15pt"        android:background="#aa0000"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:paddingLeft="10dip"        android:paddingTop="20dip"        android:paddingRight="30dip"        android:paddingBottom="40dip"        android:layout_weight="1"        android:singleLine="true"/>    <TextView        android:id="@+id/secondText"        android:text="第二行"        android:gravity="center_horizontal"        android:textSize="15pt"        android:background="#0000aa"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:padding="40dip"        android:layout_weight="1"/></LinearLayout>

清单文件:

 1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3     package="com.mars.layout01" 4     android:versionCode="1" 5     android:versionName="1.0" > 6  7     <uses-sdk 8         android:minSdkVersion="4" 9         android:targetSdkVersion="18" />10 11     <application12         android:allowBackup="true"13         android:icon="@drawable/ic_launcher"14         android:label="@string/app_name"15         android:theme="@style/AppTheme" >16         <activity17             android:name="com.mars.layout01.Layout01"18             android:label="@string/app_name" >19             <intent-filter>20                 <action android:name="android.intent.action.MAIN" />21 22                 <category android:name="android.intent.category.LAUNCHER" />23             </intent-filter>24         </activity>25     </application>26 27 </manifest>

string.xml

<?xml version="1.0" encoding="utf-8"?><resources>    <string name="app_name">Layout01</string>    <string name="action_settings">Settings</string>    <string name="hello_world">Hello world!</string></resources>

更多相关文章

  1. NestedScrollView中webview被点击,NestedScrollView会滑动问题,
  2. 自定义 SeekBar 样式 详解
  3. android Vibrator 使用
  4. Android常用控件总结
  5. Android(安卓)控件的显示隐藏上下左右移动动画
  6. android 对话框详解
  7. android笔记一 控件属性
  8. Intent调用
  9. android中进度条控件

随机推荐

  1. android > 模拟 硬件 按键 按下
  2. Android开发浏览器实例
  3. Android(安卓)scrollview嵌套listview 滑
  4. Android(安卓)开发 Eclipse 内存调整
  5. Android根据包名获取程序基本信息
  6. Android调用第三方App
  7. Android(安卓)Service 之 Bound Services
  8. 在android中使用junit
  9. Android实现模拟点击的一种方法
  10. activity_main.xml