`
oyeal
  • 浏览: 81709 次
  • 性别: Icon_minigender_2
  • 来自: 深圳
社区版块
存档分类
最新评论

控件选择性显示的设计小技巧

阅读更多
  有时候需要动态显示两个控件(三个四个甚至更多其他控件),两个控件显示时在居中位置,当显示任何其中一个控件时也显示居中,我们举例两个文本框。下面是main.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"
    >    
    <LinearLayout android:id="@+id/layout1"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:layout_marginTop="30dip">       
        <LinearLayout android:id="@+id/layout_1"
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:layout_weight="1"                    >                 
            <TextView android:id="@+id/text01"              
                android:layout_width="wrap_content"              
                android:layout_height="wrap_content"  
                android:textSize="18sp"               
                android:singleLine="true"               
                android:layout_marginLeft="10dip"            
                android:text="第一项:"
                />      
             <TextView android:id="@+id/text02"              
                android:layout_width="wrap_content"              
                android:layout_height="wrap_content"  
                android:textSize="18sp"               
                android:singleLine="true"                                             
                android:text="12345678910111213141516171819"
                />                
        </LinearLayout>

        <LinearLayout android:id="@+id/layout_2"
                    android:orientation="horizontal"
                    android:layout_width="fill_parent"                       android:layout_height="wrap_content"
                    android:gravity="center_vertical"
                    android:layout_weight="1"                     >
            <TextView android:id="@+id/text03"               
                android:textSize="18sp"
                android:singleLine="true"
                android:layout_marginLeft="10dip"
                android:layout_width="wrap_content"               
                android:layout_height="wrap_content"
                android:text="第二项:"
                />
            <TextView android:id="@+id/text04"              
                android:layout_width="wrap_content"              
                android:layout_height="wrap_content"  
                android:textSize="18sp"               
                android:singleLine="true"                                             
                android:text="12345678910111213141516171819"
                />                
        </LinearLayout>
    </LinearLayout>
       
    <LinearLayout android:id="@+id/layout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_marginTop="40dip"
    android:layout_marginLeft="40dip"
    >
   <Button android:id="@+id/Button01"
       android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="第一项"
   >
       </Button>
  <Button android:id="@+id/Button02"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="第二项"
           >
           </Button>
     <Button android:id="@+id/Button03"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="双项"
           >
           </Button>
</LinearLayout>

</LinearLayout>
  标注颜色部分很重要,可以保证控件不管是单个还是一起显示都会居中。
java文件
package com.android.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;

public class test extends Activity {
   
    ViewGroup layout_1,layout_2;
    Button btn01,btn02,btn03;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);       
        layout_1 = (ViewGroup) findViewById(R.id.layout_1);
        layout_2 = (ViewGroup) findViewById(R.id.layout_2);
        btn01 = (Button) findViewById(R.id.Button01);
        btn01.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                layout_1.setVisibility(View.VISIBLE);
                layout_2.setVisibility(View.GONE);
            }
        });
        btn02 = (Button) findViewById(R.id.Button02);
        btn02.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                layout_1.setVisibility(View.GONE);//设置控件不可见
                layout_2.setVisibility(View.VISIBLE);//设置控件可见
            }
        });
        btn03 = (Button) findViewById(R.id.Button03);
        btn03.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                layout_1.setVisibility(View.VISIBLE);
                layout_2.setVisibility(View.VISIBLE);
            }
        });
    }
}
运行结果看对应的图片。
  • 大小: 8.2 KB
  • 大小: 8.2 KB
  • 大小: 8.1 KB
分享到:
评论

相关推荐

    VBA编程技巧大全

    技巧117 使用RefEdit控件选择区域 275 技巧118 如何注册控件 276 技巧119 遍历控件的方法 279 119-1 使用名称中的变量遍历控件 279 119-2 使用对象类型遍历控件 281 119-3 使用程序标识符遍历控件 282 119-4 使用...

    VBA常用技巧

    技巧117使用RefEdit控件选择区域273 技巧118如何注册控件274 技巧119遍历控件的方法277 119-1使用名称中的变量遍历控件277 119-2使用对象类型遍历控件279 119-3使用程序标识符遍历控件280 119-4使用名称中的变量遍历...

    C#编程经验技巧宝典

    112 &lt;br&gt;0188 如何在ASP.NET中显示当前IE浏览器头信息 113 &lt;br&gt;5.6 其他应用技巧 114 &lt;br&gt;0189 如何判断年份是否为闰年 114 &lt;br&gt;0190 如何根据年份判断十二生肖 114 &lt;br&gt;0191 如何根据IP...

    Winform 界面设计 视频教程

    一些小技巧: 部分异常 全局异常 日志的重要性 门: 合理的使用控件 面向对象的方式使用控件 控件多的时候如何操作. 动态加载和组合控件 自定义控件(用户控件) 遁: 窗体美化 如何使用皮肤来美化控件...

    JSP实用技巧集合,jsp编程的一些小技巧总结

    jsp编程的一些小技巧总结,绝对实用。包括JSP编程中常用的js技术。 1.JSP编程中常用的js技术 2. 在下拉列表框里选择一个值后跳出新窗口? 3. 在JSP中启动execl? 4. 两级下拉列表框联动菜单? 5. java中如何把一个目录...

    Excel VBA实用技巧大全 附书源码

    01021改变Excel的显示位置 01022将Excel移动到屏幕以外 01023隐藏Excel 01024改变Excel的标题文字 01025删除Excel的标题文字 01026将Excel设置为全屏显示 01027在状态栏中显示信息 01028显示、隐藏状态栏 01029显示...

    delphi 开发经验技巧宝典源码

    0015 以原始风格显示控件的滚动条 11 0016 使用快捷键打开对象观察器中的“...”按钮 11 1.5 其他相关应用技巧 11 0017 安装合适的Delphi版本 11 0018 熟练掌握Delphi中的菜单项 12 0019 构成Delphi项目...

    delphi 开发经验技巧宝典源码06

    0015 以原始风格显示控件的滚动条 11 0016 使用快捷键打开对象观察器中的“...”按钮 11 1.5 其他相关应用技巧 11 0017 安装合适的Delphi版本 11 0018 熟练掌握Delphi中的菜单项 12 0019 构成Delphi项目...

    asp.net知识库

    Visual Web Development 2005开发ASP.NET使用小技巧 ASP.NET 2.0 异步页面原理浅析 [1] [原] 自定义通用System.Web.UI.IHierarchicalDataSource简单实现 在 ASP.NET 2.0 中创建 Web 应用程序主题 ASP.NET 2.0 中的...

Global site tag (gtag.js) - Google Analytics