Android——控制UI介面,androidui介面

來源:互聯網
上載者:User

Android——控制UI介面,androidui介面

一、使用XML布局檔案控制UI介面

res\layout\activity_main.xml代碼如下:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/FrameLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="@drawable/background"><TextView     android:layout_width="match_parent"    android:layout_height="wrap_content"android:text="@string/title"style="@style/text"    ></TextView><TextView    android:id="@+id/startButton"android:layout_gravity="center_vertical|center_horizontal"android:text="@string/start"android:layout_width="wrap_content"android:layout_height="wrap_content"style="@style/text"    >    </TextView></FrameLayout>

一個TextView組件用於顯示提示文字,一個在表單正中間顯示開始遊戲按鈕

其中res\values\styles.xml代碼如下

<resources xmlns:android="http://schemas.android.com/apk/res/android"><style name="text">    <item name="android:textSize">24dp</item>    <item name="android:textColor">#111111</item></style></resources>

用於指定應用的樣式,指定文字的大小和顏色。

最後在主活動中也就是MainActivity中,應用一下代碼指定應用的布局檔案。

setContentView(R.layout.activity_main);

二、用代碼來控制UI介面

package com.basil_lee.ui;import android.os.Bundle;import android.app.ActionBar.LayoutParams;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.graphics.Color;import android.util.TypedValue;import android.view.Gravity;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.FrameLayout;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//setContentView(R.layout.activity_main);FrameLayout frameLayout=new FrameLayout(this);//建立幀布局管理器frameLayout.setBackgroundDrawable(this.getResources().getDrawable(R.drawable.background));//設定背景圖片setContentView(frameLayout);//設定在Activity中顯示frameLayout//建立一個TextView組件TextView text1=new TextView(this);text1.setText("在代碼中控制UI介面");text1.setTextSize(TypedValue.COMPLEX_UNIT_PX,24);//設定文字的大小,單位為像素text1.setTextColor(Color.rgb(1, 1, 1));frameLayout.addView(text1);//建立另外一個TextView組件TextView text2=new TextView(this);text2.setText("單擊進入遊戲。。。。。。");text2.setTextSize(TypedValue.COMPLEX_UNIT_PX,50);//設定文字的大小,單位為像素text2.setTextColor(Color.rgb(1, 1, 1));LayoutParams params=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);//建立儲存布局參數的對象params.gravity=Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL;text2.setLayoutParams(params);//為text2組件添加單擊事件,並將組件添加到布局管理器中text2.setOnClickListener(new OnClickListener() {public void onClick(View arg0) {new AlertDialog.Builder(MainActivity.this).setTitle("系統提示").setMessage("遊戲有風險,進入需謹慎,真的要進入嗎?").setPositiveButton("確定", new DialogInterface.OnClickListener() {public void onClick(DialogInterface arg0, int arg1) {Toast.makeText(MainActivity.this, "進入遊戲", Toast.LENGTH_LONG).show();}}).setNegativeButton("退出",new DialogInterface.OnClickListener() {public void onClick(DialogInterface arg0, int arg1) {Toast.makeText(MainActivity.this, "離開遊戲", Toast.LENGTH_LONG).show();finish();}}).show();}});   frameLayout.addView(text2);}}

後面繼續更新。。。。。


問一下,安卓代碼控制UI介面怎做以下是我自己寫的運行後就會意外終止

把logcat貼出來啊,你這個LinearLayout沒設定長寬,會報錯。
 
Android開發,Java代碼控制UI介面,這段代碼有什錯誤?

我去,,,你這類名都錯了?還有就是匯入包,ctrl+shift+O,匯入包,然後把類名改了啊,你的檔案名稱叫xghgh,,,,但是你裡面寫的又是codeview。。。沒學過java?
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.