Android学习实践:6.应用主题和布局

本文详细介绍了如何编辑AndroidManifest.xml配置文件更改应用主题,引入兼容包,并修改MainActivity类和activity_main.xml布局文件,实现界面布局的垂直与水平排列,调整按钮样式,最终展示修改后的应用界面效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

编辑配置文件AndroidManifest.xml,加入属性android:theme如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.test1"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" 
        android:theme="@style/Theme.AppCompat.Light">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

这样就更换了应用的主题,然后我们加入下面的兼容包:

import android.support.v7.app.AppCompatActivity;

修改MainActivity的基类为AppCompatActivity:

public class MainActivity extends AppCompatActivity implements OnClickListener

运行看下效果:



接下来修改activity_main.xml,变更界面的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    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="maxwoods.demo1.MainActivity" >
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" 
        android:textColor="#0000FF"
        android:textSize="16pt"/>
     <!-- 加入button1 -->
	    <Button
	        android:id="@+id/button1"
	        android:layout_width="fill_parent"
	        android:layout_height="wrap_content"
	        android:text="@string/action_openactivity"/>
	    <!-- 加入子布局 -->
	    <LinearLayout 
	        android:layout_width="match_parent"
	        android:layout_height="wrap_content"
	        android:orientation="horizontal">
		       	<!-- 加入button2 -->
			    <Button
			        android:id="@+id/button2"
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="#FF0000"
			        android:text="@string/action_phoneCall" />
			    <!-- 加入button3 -->
			    <Button
			        android:id="@+id/button3"
			        android:layout_width="wrap_content"
			        android:layout_height="wrap_content"
					android:layout_weight="1"
					android:background="#00FF00"
			        android:text="@string/action_sendSMS" />
		</LinearLayout>
</LinearLayout>

上面将根布局改为LinearLayout,并且设置属性android:orientation="vertical",让里面的元素垂直分布,同时在button1后面嵌入了一个子设置属性LinearLayout, android:orientation="horizontal",让里面的button2,button3水平分布,这样button2,button3将作为整体与上面的元素呈垂直分布,同时还分别设置了它们的属性android:background,设置为不同的背景色,为了让这两个button等宽分布,设置它们的属性android:layout_weight="1",让它们的权重相同,保证宽度分配比例相同。而最上面的TextView,设置了其两个文本属性,android:textColor="#0000FF"和android:textSize="16pt",分别改变了颜色和尺寸,最终效果如下图:


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

螃蟹就是横着走

感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值