android布局

Android布局

当你看到android手机上的展现出漂亮的页面的时候,你就会想这是怎么做出来。其实这也不难。在每一个页面绝对会有一个相对应的布局。下面我就给大家介绍下在android开发中用到的最多的3个布局—线性布局,表格布局,相对布局。

1:线性布局(LinearLayout

       其实布局都会有2种实现方式,一种是用布局管理器xml文件,另外一种则是用代码生成。

<?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:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"à这里是要在string.xml文件定义的

    />

   <Button android:id="@+id/testButton4"

           android:layout_width="fill_parent"

           android:layout_height="wrap_content"

           android:text="@string/myRalativeLayoutButton3"/>

</LinearLayout>

以上代码线性布局的垂直布局,控制线性布局的水平与垂直是用线性的一个orientation来控制的,这样就可以达到你想要展现方式。

第二种代码生成,其实在开发中会大量的采用上面的这样的方式,但是用代码生成的,也要掌握,因为当你展现出来的控件是不确定的,当某一个按键才展现的话,这样用代码生成的话,就方便很多。

       publicclass myLinearLayout extends Activity{

    privatebooleanflag=true;

    protectedvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       final LinearLayout layout=new LinearLayout(this);

       LinearLayout.LayoutParams layoutParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.FILL_PARENT);

       layout.setOrientation(LinearLayout.VERTICAL);

      

       LinearLayout.LayoutParams textParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

       TextView tv=new TextView(this);

       tv.setLayoutParams(textParams);

       tv.setText(R.string.myLinearLayoutText);

       tv.setTextSize(20);

      

       layout.addView(tv);

      

       LinearLayout.LayoutParams buttonParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

       Button button =new Button(this);

        button.setLayoutParams(buttonParams);

       button.setText(R.string.myLinearLayoutButton1);

       layout.addView(button,buttonParams);

      

       LinearLayout.LayoutParams radioGroupParams=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

       final RadioGroup radioGroup=new RadioGroup(myLinearLayout.this);

       radioGroup.setLayoutParams(radioGroupParams);

      

       LinearLayout.LayoutParams radioButton1Parms=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

       final RadioButton radioButton1=new RadioButton(myLinearLayout.this);

       radioButton1.setId(1);

       radioButton1.setLayoutParams(radioButton1Parms);

       radioButton1.setText(R.string.maleText);

       radioButton1.setChecked(true);

       radioGroup.addView(radioButton1);

      

       LinearLayout.LayoutParams radioButton2Params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);

       final RadioButton radioButton2=new RadioButton(myLinearLayout.this);

       radioButton2.setId(2);

       radioButton2.setLayoutParams(radioButton2Params);

       radioButton2.setText(R.string.famaleText);

       radioGroup.addView(radioButton2);

      

       layout.addView(radioGroup);

      

       radioGroup.setVisibility(View.GONE);

      

       super.setContentView(layout,layoutParams);

      

       button.setOnClickListener(new View.OnClickListener(){

           publicvoid onClick(View v) {

              if(flag){

                  radioGroup.setVisibility(View.VISIBLE);

                  flag=false;

              }else{

                  radioGroup.setVisibility(View.GONE);

                  flag=true;

              }

             

              radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){

                  publicvoid onCheckedChanged(RadioGroup group, int checkedId) {

                     if(checkedId==1){

                         radioButton2.setChecked(true);

                     }else{

                         radioButton1.setChecked(true);

                     }

                    

                  }

              });

             

             

           }

       });

      

    }

}

在以上的代码中在一个页面出现了一个 TextView ,一个button,在就是用button控制展现的一个单选框(单选框的展现和隐藏)。

其中GONEVISIBLE 分别用来隐藏和展现。

 

2:表格布局(TableLayout

  当你打开你的android手机的时候,展现在你面前的就是一个表格布局,下面也是用2种的方式展现出table布局

<?xml version="1.0" encoding="utf-8"?>

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

    android:layout_width="fill_parent" android:layout_height="fill_parent"

    android:orientation="vertical"

    android:id="@+id/myTableLayoutId"

    android:shrinkColumns="3"

    android:background="@drawable/psu">

   

    <View android:layout_height="2px"

         android:background="#FF909090" />

    <TableRow>

       <TextView android:layout_column="0"

                android:gravity="center_vertical"

                android:padding="8px"

                android:text="ID"/>

       <TextView android:layout_column="1"

                android:gravity="center_vertical"

                android:padding="8px"

                android:text="name"/>

       <TextView android:layout_column="2"

                android:gravity="center_vertical"

                android:padding="8px"

                android:text="email"/>

       <TextView android:layout_column="3"

                android:padding="8px"

                android:gravity="center_vertical"

                android:text="address"/>

    </TableRow>

</TableLayout>

table布局中往往你遇到,一行展现出来的东西会有一部分不能显示出来,在android中它会用shrinkColumns它会用这个来控制第几行来换行。其实table布局和javaweb中的table很相似,在table中要有TableRow 这个就相当于table中的tr。你可以控制每一个TableRow内容的的居中,居左,居右等等。

这个是使用android:gravity

代码的实现部分,

publicclass myTableLayoutTwo extends Activity {

    private Map<String, String> mapTitle = new HashMap<String, String>();

    private List<Map<String, String>> listData = new ArrayList<Map<String, String>>();

 

    protectedvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       // 组装数据表头

       this.mapTitle.put("id", "编号");

       this.mapTitle.put("name", "姓名");

       this.mapTitle.put("cj", "成绩");

       // 组装数据内容

       for (int i = 0; i < 3; i++) {

           Map<String, String> map = new HashMap<String, String>();

           map.put("id", String.valueOf(i));

           map.put("name", "bruce" + i);

           map.put("cj", String.valueOf(i));

           this.listData.add(map);

       }

 

       TableLayout layout = new TableLayout(this);

       TableLayout.LayoutParams tableLayout = new TableLayout.LayoutParams(

              ViewGroup.LayoutParams.FILL_PARENT,

              ViewGroup.LayoutParams.FILL_PARENT);

       layout.setBackgroundResource(R.drawable.psu);

      

       TableRow tableRowTitle=new TableRow(this);

       for(int i=0;i<this.mapTitle.size();i++){

           String key="";

           switch (i) {

           case 0:

              key="id";

              break;

           case 1:

              key="name";

              break;

           case 2:

              key="cj";

               break;

           default:

              break;

           }

           TextView textViewTitle=new TextView(this);

           textViewTitle.setText(this.mapTitle.get(key));

           tableRowTitle.addView(textViewTitle);

       }

       layout.addView(tableRowTitle);

       System.out.println("33333333");

       int k=1;

       for(int i=0;i<this.listData.size();i++){

           k++;

           Map<String,String> map=this.listData.get(i);

           TableRow tableRow=new TableRow(this);

           for(int j=0;j<map.size();j++){

              String key="";

              switch (j) {

              case 0:

                  key="id";

                  break;

              case 1:

                  key="name";

                  break;

              case 2:

                  key="cj";

                  break;

              default:

                  break;

              }

              TextView textViewContent=new TextView(this);

              textViewContent.setText(map.get(key));

              tableRow.addView(textViewContent,j);

           }

           layout.addView(tableRow);

       }

       super.setContentView(layout,tableLayout);

    }

 

}

 

在用代码实现tablelayout的时候特别要注意,tableRow.addView(textViewContent,j);里面的内容确实是在一行中,比如动态加载数据的时候。这样就会遇到这样的就问题,上面的例子就是用j来控制的。来表示是一行的内容。

3:相对布局(RelativeLayout

    顾名思义相对布局就要有一个参照物,这样你才能相对它来布局你的主键

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

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

  android:id="@+id/myRelativeLayoutId"

  android:layout_width="wrap_content"

  android:layout_height="wrap_content">

 

  <ImageView android:id="@+id/imga"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:src="@drawable/android_mldn_01"/>

 

  <ImageView android:id="@+id/imgb"

            android:layout_height="wrap_content"

            android:layout_width="wrap_content"

            android:src="@drawable/android_mldn_02"

            android:layout_toRightOf="@id/imga"/>

</RelativeLayout>

以上是2张图片的相对布局是把idimgb的放在imga的右边,在上面的布局中是用toRightOf来控制的。

publicclass myRelativeLayout extends Activity {

    private RelativeLayout myRelativeLayout;

 

    protectedvoid onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       super.setContentView(R.layout.myralativelayout);

       this.myRelativeLayout = (RelativeLayout) super

              .findViewById(R.id.myRelativeLayoutId);

 

       RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(

              ViewGroup.LayoutParams.FILL_PARENT,

              ViewGroup.LayoutParams.WRAP_CONTENT);

       relativeLayoutParams.addRule(RelativeLayout.RIGHT_OF, R.id.imga);

       relativeLayoutParams.addRule(RelativeLayout.BELOW, R.id.myT1);

       Button button=new Button(this);

       button.setText("代码生成的button");?????不要这样写

       this.myRelativeLayout.addView(button,relativeLayoutParams);

      

    }

 

}

注意在上面的代码中有一些不合理的地方,就是在手机应用避免出现硬编码,就是button.setText("代码生成的button");最好是在string.xml文件中定义文字,这样你好统一的去改这些说明文字。可以写成button.setText(R.string.maleText)

当在学习android的时候你要注意一些性能上的问题,因为手机的内存有限,cpu没有那么的强大,在运行起来要达到快速,这样就会要求我们在开发中选择布局是用代码生成,还是用xml生成,这样就要看具体的应用方式。本人建议你用xml文件生成。

在开发中大部分用的就是上面的三种布局,在加上布局的嵌套就会达到你想要的效果

原文地址:https://www.cnblogs.com/snake-hand/p/3161517.html