无涯教程-Android - Style Demo Example函数

这篇具有很好参考价值的文章主要介绍了无涯教程-Android - Style Demo Example函数。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

下面的示例演示如何将样式用于单个元素。让我们开始按照以下步骤创建一个简单的Android应用程序-

步骤 说明
1 您将使用Android Studio IDE创建一个Android应用程序,并在 com.example.saira_000.myapplication 包下将其命名为 myapplication ,如中所述您好世界Example一章。
2 修改 src/MainActivity.java 文件以为定义的按钮添加点击事件监听器和处理程序
3 在全局样式文件 res/values/style.xml 中定义样式,以定义按钮的自定义属性。
4 修改 res/layout/activity_main.xml 文件的默认内容,以包括一组Android UI控件并使用已定义的样式。
5 运行该应用程序以启动Android模拟器并验证在该应用程序中所做的更改的输出。

以下是修改后的主要活动文件 src/com.example.myapplication/MainActivity.java 的内容。该文件可以包括每个基本生命周期方法。

package com.example.saira_000.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {
   Button b1;
   
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      
      b1=(Button)findViewById(R.id.button);
      b1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Toast.makeText(MainActivity.this,"YOUR MESSAGE",Toast.LENGTH_LONG).show();
         }
      });
   }
}

以下是 res/values/style.xml 文件的内容,该文件的附加样式 CustomButtonStyle 已定义-

<resources>

   <!-- Base application theme. -->
   
   <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
      <!-- Customize your theme here. -->
		<item name="colorPrimary">@color/colorPrimary</item>
      <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
      <item name="colorAccent">@color/colorAccent</item>
   </style>
   
   <style name="CustomButtonStyle">
      <item name="android:layout_width">100dp</item>
      <item name="android:layout_height">38dp</item>
      <item name="android:capitalize">characters</item>
      <item name="android:typeface">monospace</item>
      <item name="android:shadowDx">1.2</item>
      <item name="android:shadowDy">1.2</item>
      <item name="android:shadowRadius">2</item>
      <item name="android:textColor">#000000</item>
      <item name="android:gravity" >center</item>
      <item name="android:layout_margin" >3dp</item>
      <item name="android:textSize" >5pt</item>
      <item name="android:background">#70ff106d</item>
      <item name="android:shadowColor" >#70ff106d</item>
   </style>

</resources>

以下是 res/layout/activity_main.xml 文件的内容-

Here abc indicates about learnfk logo
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" 
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
   >

   <TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Custom Button Style "
      android:layout_alignParentTop="true"
      android:layout_centerHorizontal="true"
      android:textSize="30dp" />

   <TextView
      android:id="@+id/textView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Tutorials point "
      android:textColor="#ff87ff09"
      android:textSize="30dp"
      android:layout_below="@+id/textView1"
      android:layout_centerHorizontal="true" />

   <ImageButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/imageButton"
      android:src="@drawable/abc"
      android:layout_below="@+id/textView2"
      android:layout_centerHorizontal="true" />

   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      style="@style/CustomButtonStyle"
      android:text="New Button"
      android:id="@+id/button"
      android:layout_below="@+id/imageButton"
      android:layout_alignLeft="@+id/imageButton"
      android:layout_alignStart="@+id/imageButton"
      android:layout_alignRight="@+id/textView2"
      android:layout_alignEnd="@+id/textView2" />

</RelativeLayout>

以下是 res/values/strings.xml 的内容,以定义两个新的常量-

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <string name="app_name">myapplication</string>
</resources>

以下是 AndroidManifest.xml 的默认内容-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.guidemo">
      
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android:name="com.example.saira_000.myapplication"
         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>

让我们尝试运行您的 myapplication 应用程序。我假设您在进行环境设置时创建了 AVD 。要从Android Studio运行该应用程序,请打开您项目的活动文件之一,然后单击"运行工具栏。 Android studio将应用程序安装在您的AVD上并启动它,如果设置和应用程序一切正常,它将在"模拟器"窗口中显示-

如果您单击上面的按钮,它将显示吐司消息

Android 中的 Style Demo Example函数 - 无涯教程网无涯教程网提供下面的示例演示如何将样式用于单个元素。让我们开始按照以下步骤创建一个简单的Androi...https://www.learnfk.com/android/android-style-demo-example.html文章来源地址https://www.toymoban.com/news/detail-690566.html

到了这里,关于无涯教程-Android - Style Demo Example函数的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 无涯教程-Android - AutoCompleteTextView函数

    AutoCompleteTextView是一个类似于EditText的视图,只是它在用户键入时自动显示补充数据。 以下是与AutoCompleteTextView控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。 Sr.No Attribute 描述 1 android:completionHint 这定义了在

    2024年02月10日
    浏览(28)
  • 无涯教程-Android - ToggleButton函数

    ToggleButton将已选中/未选中状态显示为按钮。它基本上是一个带有指示灯的开/关按钮。 以下是与ToggleButton控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。 Sr.No. Attribute 描述 1 android:disabledAlpha 这是禁用时应用

    2024年02月10日
    浏览(23)
  • 无涯教程-Android - CheckBox函数

    CheckBox是可以由用户切换的on/off开关。为用户提供一组互不排斥的可选选项时,应使用复选框。 以下是与CheckBox控件相关的重要属性。您可以查看Android官方文档以获取属性的完整列表以及可以在运行时更改这些属性的相关方法。 继承自 android.widget.TextView 类- Sr.No Attribute 描述

    2024年02月10日
    浏览(27)
  • 无涯教程-Android - RadioButton函数

    RadioButton有两种状态:选中或未选中,这允许用户从一组中选择一个选项。 本示例将带您完成一些简单的步骤,以展示如何使用Linear Layout和RadioButton创建自己的Android应用程序。 以下是修改后的主要Activity文件 src/MainActivity.java 的内容。 以下是 res/layout/activity_main.xml 文件的内容- 以

    2024年02月10日
    浏览(22)
  • 无涯教程-Android - Absolute Layout函数

    Absolute Layout 可让您指定其子级的确切位置(x/y坐标),绝对布局的灵活性较差且难以维护。 以下是AbsoluteLayout特有的重要属性- Sr.No Attribute 描述 1 android:id 这是唯一标识布局的ID。 2 android:layout_x 这指定视图的x坐标。 3 android:layout_y 这指定视图的y坐标。 本示例将带您完成简单的

    2024年02月10日
    浏览(23)
  • 无涯教程-Android - List View函数

    Android ListView 是垂直滚动列表中显示的视图,使用 Adapter 从列表(如数组或数据库)中获取内容的列表项会自动插入列表中。 适配器(Adapter)实际上是UI组件和将数据填充到UI组件中的数据源之间的桥梁,适配器保存数据并将数据发送到适配器视图,该视图可以从适配器视图获取数

    2024年02月10日
    浏览(25)
  • 无涯教程-Android - Frame Layout函数

    Frame Layout 旨在遮挡屏幕上的某个区域以显示单个项目,通常,应使用FrameLayout来保存单个子视图,因为在子视图彼此不重叠的情况下,难以以可扩展到不同屏幕尺寸的方式组织子视图。 不过,您可以使用android:layout_gravity属性将多个子项添加到FrameLayout并通过将gravity分配给每

    2024年02月10日
    浏览(23)
  • 无涯教程-Android - List fragments函数

    框架的ListFragment的静态库支持版本,用于编写在Android 3.0之前的平台上运行的应用程序,在Android 3.0或更高版本上运行时,仍使用此实现。 List fragment 的基本实现是用于创建fragment中的项目列表 本示例将向您说明如何基于arrayAdapter创建自己的列表片段,在开始编码之前,我将在

    2024年02月11日
    浏览(27)
  • 无涯教程-Android Online Test函数

    Android在线测试模拟了真正的在线认证考试。您将看到基于 Android概念的多项选择题(MCQ),将为您提供四个options。您将为该问题选择最合适的答案,然后继续进行下一个问题,而不会浪费时间。完成完整的考试后,您将获得在线考试分数。 总问题数-20 最长时间-20分钟 Start Test Androi

    2024年02月10日
    浏览(23)
  • 无涯教程-Android Intent Standard Extra Data函数

    下表列出了各种重要的Android Intent Standard Extra Data。您可以查看Android官方文档以获取额外数据的完整列表- Sr.No Extra Data Description 1 EXTRA_ALARM_COUNT 用作AlarmManager intents(意图)中的int Extra字段,以告诉正在调用的应用程序intents(意图)释放了多少未决警报。 2 EXTRA_ALLOW_MULTIPLE 用于指示

    2024年02月10日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包