您的位置:首页 > 移动开发 > Android开发

Android的垂直布局管理器的应用

2015-09-16 18:15 281 查看
1.在layout中建立一个XML文件

右击layout文件夹在其中新建一个Android XMLX Fil饿>>>填写一个file名选择一个布局管理器LinearLayout

2.在XML文件中将所要的效果做出来

XML文件中有两个:一个是.xml,另外一个是Layout布局管理器

<?xml version="1.0" encoding="utf-8"?>  //?xml,表示xml文件。version="1.0"是版本号,如微信1.0版本。encoding="utf-8"是编码集

A.XML文件的格式

a.<LinearLayout  中间写类容1>中间写类容2</LinearLayout>  中间写类容1是LinearLayout的相关内容,中间写类容2是其他内容,其他的XML的东西。

b. <TextView  中间写类容 />

eg.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:background="@android:color/holo_green_light"

    android:orientation="vertical" >

    <TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"

        android:layout_weight="1"

        android:background="@android:color/holo_orange_light"

        android:text="@string/first_button"

        android:textColor="@android:color/holo_purple"

        android:textSize="30sp" />

</LinearLayout>

B.布局管理器的代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"//自动生成的。

    android:layout_width="match_parent"//layout_width:布局管理器的宽  match_parent:匹配父布局管理器,在这表明覆盖全屏

    android:layout_height="match_parent"//layout_height:布局管理器的高  

    android:background="@android:color/holo_green_light"//background:背景颜色

    android:orientation="vertical" >//orientation:布局方式,vertical垂直布局   horizontal:水平布局

</LinearLayout>

C.文本代码

<TextView

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginTop="10dp"//距顶点的距离  距离的符号是dp

        android:layout_weight="1"

        android:background="@android:color/holo_orange_light"

        android:text="@string/first_button"

        android:textColor="@android:color/holo_purple"

        android:textSize="30sp" />//文本的大小  大小的符号是dp

补充:android:text="@string/first_button"

先在values文件夹中找到Strings.xml,并打开,会出现

  <string name="app_name">menu</string>

  <string name="hello_world">Hello world!</string>

  <string name="action_settings">Settings</string>

按照上面的格式仿建

<string name = "first_button">元素一</string>  <string name = "second_button">元素二</string>

  <string name = "three_button">元素三</string>

做了之后就可以在@string中找到想要的东西

D.保存就可以在Layout布局管理器中看到效果

E.将XML文件链接到“主函数”中

在src中新建一个包,再新建一个类,该类extends Activity
后出现

public class LinearLayout extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.linear_layout);//将XML文件链接到“主函数”中的
方法
}

}

其中要改R在的包

F。插入安卓手机进行调试

调试时要修改AndroidManifest.xml中
package="com.scxh.weixin.ui"和
android:name=".LinearLayout"

改为要运行的程序相关类容。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: