Android里面怎么让界面居中

(1)TableLayout:

可以使用

android:stretchColumns="0,3"使左右最边上两个列允许别拉伸

例如:

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tablelayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/loginbg"
    android:gravity="center_vertical"
    android:stretchColumns="0,3" >

    <!--
    android:stretchColumns="0,3"
   	 第一列和第四列设置允许被拉伸,是为了让用户登录表单在水平方向上居住显示
    -->
    <!-- 第一行 -->

    <TableRow
        android:id="@+id/tablerow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView />

        <TextView
            android:id="@+id/textview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="用户名:" />

        <EditText
            android:id="@+id/edittext1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:minWidth="200px"
            android:textSize="24px" />

        <TextView />
    </TableRow>
    <!-- 第二行 -->

    <TableRow
        android:id="@+id/tablerow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView />

        <TextView
            android:id="@+id/textview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="密码"
            android:textSize="24px" />

        <EditText
            android:id="@+id/edittext2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:minWidth="200px"
            android:textSize="24px" />

        <TextView />
    </TableRow>

    <TableRow
        android:id="@+id/tablerow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <TextView />

        <TextView />

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="登录"
            android:textSize="24px" />

        <TextView />
    </TableRow>

</TableLayout>

RelativeLayout 内容居中解决办法:

使用Linearlayout本来利用父控件的gravity属性是很好解决的。

但是对应RelativeLayout虽然有gravity属性,

但是如果你使用,你会发现实际他是不能生效的。

解决办法:

在RelativeLayout的子空间里,使用以下3个属性就行了。

android:layout_centerVertical="true"

android:layout_centerHorizontal="true"

android:layout_centerInParent="true"

gravity 一般是针对linearlayout会使用很方便。

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 21:45:35

Android里面怎么让界面居中的相关文章

android实现仿QQ界面刷新

android实现仿QQ界面刷新 转载请注明出处:http://blog.csdn.net/wangpengfei_p/article/details/51420422 昨天想要实现一个下拉刷新的效果,本来想应该比较简单,因为之前在慕课网看见过类似的实现,记得是在listView里面添加footView或是添加headView,监听手指的点击滑动事件来控制view的显示或是隐藏,但是自己按照上面的代码来实现之后发现.这样做有一点不好的地方: 它判断是否刷新的依据是判断listView是不是滑动到

Android Launcher 如何实现壁纸居中

前言          欢迎大家我分享和推荐好用的代码段~~ 声明          欢迎转载,但请保留文章原始出处: CSDN:http://www.csdn.net 雨季o莫忧离:http://blog.csdn.net/luckkof 正文 KK Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,只能确保第一次开机时壁纸居中,后面用户修改桌面数目后,就无法达到了.如何要在默认桌面数目配置时居中,可以参考下面方法,以default一屏为例:

Android 自学之基本界面组件(下)

按钮(Button)与图片按钮(ImageButton)组件的功能和用法 Button继承了TextView,ImageButton继承了Button.不管是Button还是ImageButton,他们的功能都很单一,主要是在UI界面生成一个按钮,该按钮可以供用户单击,当用户单击按钮后出发一个Onclick事件. Button  和  ImageButton的不同在于Button生成的按钮显示文字,ImageButton生成的按钮显示图片.(关于ImageButton属性android:text

【Android开发-5】界面装修,五大布局你选谁

前言:如果要开一家店,门店装修是很重要的事情.有钱都请专门的建筑设计公司来设计装修,没钱的只能自己瞎折腾,好不好看全凭自己的感觉.像Android开发,在移动端大家看到的界面视觉不咋滴,一般连打开的动力都没了.所以Android开发就有了专门的UI设计人员,既然有了UI设计图,那怎么布局就需要靠自己去选择了,五大布局中可以随意选,只要能达到你的UI设计图的效果.设计图给你了,你选哪位装修工给你装修,就看效率了:不用说,我们都选择效率高的来装修. Android的五大布局: 1.线性布局(Line

Android开机启动Activity界面

1,原理: 当Android系统完成BOOT阶段之后,就会发送一条名为 ACTION_BOOT_COMPLETED 的广播,我们便可在一个BroadcastReceiver中捕获这条广播,然后启动我们的Activity或者Service,当然要注意的是,我们的application必须具有捕获该广播的权限,下面请看具体步骤: 第一步:首先要有一个用于开机启动的Activity或者Service,这里以系统自己创建的最简单的Activity为例进行讲解. package com.billhoo.s

android studio 新建项目 界面一直停在 【“building ‘ 项目名’ gradle project info”】

zhezhelin android studio 新建项目 界面一直停在 [“building ‘ 项目名’ gradle project info”] 安装了android studio 之后,按照上文所述的那篇博文下载安装gradle,配置环境变量, 启动android studio,新建项目,发现还是新建不了,界面一直停在 [“building ‘ 项目名’ gradle project info”]: 其实这时候android studio 还是在下载 gradle ,但是由于被墙的原因

Android应用经典主界面框架之一:仿QQ (使用Fragment, 附源码)

最近反复研究日常经典必用的几个android app,从主界面带来的交互方式入手进行分析,我将其大致分为三类.今天记录第一种方式,即主界面下面有几个tab页,最上端是标题栏,tab页和tab页之间不是通过滑动切换的,而是通过点击切换tab页.早期这种架构一直是使用tabhost+activitygroup来使用,随着fragment的出现及google官方也大力推荐使用fragment,后者大有代替前者之势.本文也使用fragment进行搭建,标题中的"经典"指这种交互经典,非本文的代

精美的 ( Android, iPhone, iPad ) 手机界面设计素材和线框图设计工具

在制作界面原型的时候,如果有现成的界面基础元素可以使用的话,设计师就可以非常快速的完成原型的制作,能够节省大量的时间和精力.在这篇文章, 我向大家分享45套非常有用的 UI 和 Wireframe 套件素材包,帮助设计们用于 Web 和 Mobile 项目的界面原型设计. Android 2.2 GUI 整套 Android 2.2 手机界面 UI 组件素材,非常精美! Big Block UI 下拉列表,开关切换按钮,搜索框以及单选,多选按钮等. Stencil kit 滑块控制,开关按钮等组

android异步更新UI界面的方法

在android平台下,进行多线程编程时,经常需要在主线程之外的一个单独的线程中进行某些处理,然后更新用户界面显示.但是,在主线线程之外的线程中直接更新页面显示的问题是:系统会报这个异常,android.view.viewroot$calledfromwrongthreadexception: only the original thread that created a view hierarchy can touch its views. (只有原始创建这个视图层次(view hierach