Android学习之旅——android中布局管理器的总结

来源:互联网 发布:淘宝网天猫大码女装 编辑:程序博客网 时间:2024/05/19 02:22

            对于一个应用来说,好的交互性界面会带来不一样的用户体验,良好的用户体验会给你的应用带来用户群,这是每一个开发者都愿意看到的。对于Android开发者来说也是这样,如何将界面做的更好,一个很基本的要求就是深入研究布局管理器。下面我们将Android中的布局管理器做一个总结。

           Android中提供了五种布局管理器,分别是线性布局管理器,框架布局管理器,表格布局管理器,相对布局管理器和绝对定位布局管理器。其中绝对定位布局管理器在Android2.3.3版本后被废除了,所以我们只总结前四种布局管理器。

          (一)线性布局管理器:LinearLayout

                   使用线性布局管理器管理的组件要么水平排列,要么垂直排列,两种方式的排列都是在一条线上,当排列的组件超过设备的宽度后组件可能不可见,LinearLayout有两种使用方式,一种是通过配置文件进行定义,一种是通过程序来操作

                  1,使用配置文件

                       使用配置文件定义的布局管理器代码如下:

                       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                 xmlns:tools="http://schemas.android.com/tools"
                                 android:orientation="horizontal"
                                 android:layout_width="match_parent"
                                 android:layout_height="match_parent"
                                 android:paddingBottom="@dimen/activity_vertical_margin"
                                 android:paddingLeft="@dimen/activity_horizontal_margin"
                                android:paddingRight="@dimen/activity_horizontal_margin"
                               android:paddingTop="@dimen/activity_vertical_margin"
                              tools:context=".LinearLayoutDemoActivity" >
                     <TextView
                             android:layout_width="wrap_content"
                             android:layout_height="wrap_content"
                             android:text="@string/hello_world" />
</LinearLayout>

                 2,通过程序直接生成布局管理器

                      使用程序直接生成布局管理器需要定义布局管理器以及托管在管理器的组件的布局参数,此参数通过LinearLayout.LayoutParams来实现,具体的代码如下;

                      protected void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
                      LinearLayout layout = new LinearLayout(this) ;
                     LinearLayout.LayoutParams layoutparam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,                   ViewGroup.LayoutParams.FILL_PARENT) ;
                    LinearLayout.LayoutParams txtparam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) ;
                    TextView text = new TextView(this) ;
                   layout.setOrientation(LinearLayout.HORIZONTAL) ;
                   layout.addView(text, txtparam) ;
                 super.setContentView(layout, layoutparam) ;
}

                  (二)框架布局管理器:FrameLayout

                         通过框架布局管理器进行组件的布局,就是在屏幕的左上角开辟一片区域用来布局组件,采用这种方法所有的组件层叠显示,,也就是说组件会出现叠加的情况。

                         FrameLayout的生成方式与LinearLayout类似,也有两种生成方式,一种是通过配置文件生成,一种是通过程序生成。

                        1,通过配置文件生成,代码如下:

                            <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                          xmlns:tools="http://schemas.android.com/tools"
                                         android:layout_width="match_parent"
                                         android:layout_height="match_parent"
                              >
                                         <TextView
                                                   android:layout_width="wrap_content"
                                                  android:layout_height="wrap_content"
                                                  android:text="@string/hello_world" />
                                       <Button
                                                android:id="@+id/button"
                                               android:layout_width="fill_parent"
                                               android:layout_height="wrap_content"
                                              android:text="press me"
                                        >
                                     </Button>
                                    <ImageView
                                            android:id="@+id/image"
                                            android:layout_width="fill_parent"
                                            android:layout_height="wrap_content"
                                            android:src="@drawable/ic_launcher"
                                    >   
                                 </ImageView>
                                 <EditText
                                       android:id="@+id/edit"
                                       android:text="please input"
                                      android:layout_width="fill_parent"
                                     android:layout_height="wrap_content" 
                                 >     
                               </EditText>      
                      </FrameLayout>

                   2.通过程序生成如下:

                       protected void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    FrameLayout framelayout = new FrameLayout(this) ;
                    FrameLayout.LayoutParams layoutparam = new                      FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT) ;
                    FrameLayout.LayoutParams viewLayout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT) ;
                    Button button = new Button(this) ;
                    button.setText("press me") ;
                    ImageView image = new ImageView(this) ;
                    image.setImageResource(R.drawable.ic_launcher) ;
                   EditText edit = new EditText(this) ;
                   edit.setText("input data") ;
                   framelayout.addView(button, viewLayout) ;
                   framelayout.addView(image, viewLayout) ;
                 framelayout.addView(edit, viewLayout) ;
                super.setContentView(framelayout,layoutparam) ;

                    }

                    (三)表格布局管理器:TableLayout

                              在Android中可以通过表格的形式对界面进行管理,这要通过TableLayout来实现。在TableLayout中要通过TableRow来对界面的行进行控制,之后所有的组件都要在TableRow中增加。TableLayout的使用方式也有两种,一种通过配置文件来实现,一种是通过程序来生成。

                           1.通过配置文件来生成表格布局管理器,代码如下:

                              <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                           xmlns:tools="http://schemas.android.com/tools"
                                          android:id="@+id/tablelayout"
                                          android:layout_width="match_parent"
                                          android:layout_height="match_parent"
                              >
                           <TableRow>
                                      <EditText
                                                android:id="@+id/edit"
                                                android:layout_width="wrap_content"
                                                android:layout_height="wrap_content"
                                                android:text="please input data"
                                      >
                                   </EditText>
                                   <Button
                                                android:id="@+id/button"
                                               android:layout_width="wrap_content"
                                               android:layout_height="wrap_content"
                      android:text="search"
                                     >
                               </Button>
                    </TableRow>
                   <TableRow>
                               <TextView
                              android:id="@+id/textview"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                          android:text="please choose sex of yourself"
                           >
                          </TextView>
                         <RadioGroup
                                        android:id="@+id/radiogroup"
                                        android:layout_width="fill_parent"
                                        android:layout_height="wrap_content"
                                       android:orientation="vertical"
                                      android:checkedButton="@+id/male" 
                        >
                     <RadioButton
                                          android:id="@+id/male"
                                         android:text="male"
                    />
       <RadioButton
                                    android:id="@+id/female"
                                    android:text="female"
                     />
                  </RadioGroup>
                    </TableRow>
                </TableLayout>

                 (四)相对布局管理器:RelativeLayout

                           相对布局管理器是指参考某一控件对组件进行摆放,可以通过控制将组件摆放在一个指定的组件的上下左右等位置。

                         1. 通过配置文件生成相对布局管理器的代码如下:

                           <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                      xmlns:tools="http://schemas.android.com/tools"
                                      android:id="@+id/relativelayout"
                                      android:layout_width="match_parent"
                                      android:layout_height="match_parent"
                            >
                          <TextView
                                     android:id="@+id/text01"
                                     android:layout_width="wrap_content"
                                     android:layout_height="wrap_content"
                                     android:text="@string/hello_world" />
                        <TextView
                                    android:id="@+id/text02"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_toRightOf="@id/text01"
                                    android:text="hello"
                        >
                     </TextView>
                      <Button
                                   android:id="@+id/button"
                                   android:layout_width="wrap_content"
                                   android:layout_height="wrap_content"
                                   android:text="press me"
                                   android:layout_below="@id/text02"
                     >
                   </Button>
                  </RelativeLayout>

                 2。通过程序生成相对布局管理器,代码如下:

                     protected void onCreate(Bundle savedInstanceState) {
               super.onCreate(savedInstanceState);
               setContentView(R.layout.activity_relative_layout_demo);
               RelativeLayout relative = (RelativeLayout)super.findViewById(R.id.relativelayout) ;
               RelativeLayout.LayoutParams param = new            RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT) ;
              param.addRule(RelativeLayout.ABOVE,R.id.button) ;
              EditText edit = new EditText(this) ;
              relative.addView(edit,param) ;
              } 

还有一种绝对定位布局管理器由于现在不适用或使用的较少,所以不对其做总结。

 

            

                              

0 0
原创粉丝点击