zoukankan      html  css  js  c++  java
  • 控件布局_LinearLayout

    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     <application
    12         android:allowBackup="true"
    13         android:icon="@drawable/ic_launcher"
    14         android:label="@string/app_name"
    15         android:theme="@style/AppTheme" >
    16         <activity
    17             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>
  • 相关阅读:
    C#多态联系之虚方法
    FileStream 操作文件复制
    [Android] Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.LoaderCallbacks)
    [Android] Android 异步定时任务实现的三种方法(以SeekBar的进度自动实现为例)
    [Android] Android Butterknife 8.8.1 在 Activity 和 Fragment 、 Adapter 中的使用
    [Android] Android v4包CompoundButtonCompatLollipop.class重复问题
    [Android] Android 常见第三方库汇总地址
    [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)
    [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)
    [Android] Android : lambda expressions are not supported at this language level(需设置project language level)
  • 原文地址:https://www.cnblogs.com/LO-ME/p/3584499.html
Copyright © 2011-2022 走看看