生活随笔
收集整理的這篇文章主要介紹了
【Android开发】我的第一个安卓程序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
小技巧
在xml中設置控件寬度為父窗口的一半
<LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:weightSum="1.0"><Buttonandroid:text="left"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight=".50"/><Buttonandroid:text="right"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight=".50"/></LinearLayout>
代碼
1、線性排列 LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".MainActivity"android:orientation="vertical"><LinearLayoutandroid:id="@+id/ll_1"android:layout_width="200dp"android:layout_height="200dp"android:background="#00C1FF"android:orientation="vertical"android:paddingLeft="30dp"android:paddingRight="30dp"android:paddingBottom="20dp"android:paddingTop="10dp"android:layout_marginBottom="20dp">//內部元素的邊距
<Viewandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#9D9DF7"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="200dp"android:background="#FFE65C"android:orientation="horizontal"android:layout_marginTop="20dp"android:layout_marginLeft="15dp"android:layout_marginRight="15dp"android:gravity="center"><Viewandroid:layout_width="0dp"android:layout_height="80dp"android:background="#FF9188"android:layout_weight="1"/>//剩余部分權重
<Viewandroid:layout_width="0dp"android:layout_height="80dp"android:background="#FFF5F5"android:layout_weight="1"/><Viewandroid:layout_width="0dp"android:layout_height="80dp"android:background="#C5E9AC"android:layout_weight="1"/></LinearLayout>//TextView是一個用來展示文本的控件 整個文件存放的是布局文件
<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World! 這是我的第一個安卓應用"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintTop_toTopOf="parent" /></LinearLayout>
2、相對位置排列 RelativeLayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"><Viewandroid:id="@+id/view_1"android:layout_width="100dp"android:layout_height="100dp"android:layout_alignParentLeft="true"android:layout_alignParentTop="true"android:background="#FFC107" /><Viewandroid:id="@+id/view_2"android:layout_width="100dp"android:layout_height="100dp"android:layout_toRightOf="@id/view_1"android:background="#6495ED" /><Viewandroid:id="@+id/view_3"android:layout_width="100dp"android:layout_height="100dp"android:layout_below="@id/view_1"android:background="#FF4836" /><LinearLayoutandroid:layout_width="match_parent"android:layout_height="200dp"android:layout_below="@id/view_3"android:background="#3F51B5"android:orientation="horizontal"android:padding="10dp"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:background="#383333"android:padding="15dp"><Viewandroid:id="@+id/view_6"android:layout_width="100dp"android:layout_height="80dp"android:layout_weight="1"android:background="#FFFFFF" /><Viewandroid:id="@+id/view_7"android:layout_width="100dp"android:layout_height="80dp"android:layout_toRightOf="@id/view_6"android:layout_weight="1"android:background="#B1A6A6" /></RelativeLayout></LinearLayout>
</RelativeLayout>
3、TextView
真機運行圖
代碼
activity_main.xml
TextViewActivity.java
package com
.hanquan
.helloworld
;import androidx
.appcompat
.app
.AppCompatActivity
;import android
.graphics
.Paint
;
import android
.os
.Bundle
;
import android
.text
.Html
;
import android
.widget
.TextView
;public class TextViewActivity extends AppCompatActivity {private TextView mTv4
;private TextView mTv5
;private TextView mTv6
;private TextView mTv7
;private TextView mTv8
;@Overrideprotected void onCreate(Bundle savedInstanceState
) {super.onCreate(savedInstanceState
);setContentView(R
.layout
.activity_text_view
);mTv4
= findViewById(R
.id
.tv_4
);mTv4
.getPaint().setFlags(Paint
.STRIKE_THRU_TEXT_FLAG
);mTv4
.getPaint().setAntiAlias(true);mTv5
= findViewById(R
.id
.tv_5
);mTv5
.getPaint().setFlags(Paint
.STRIKE_THRU_TEXT_FLAG
);mTv6
= findViewById(R
.id
.tv_6
);mTv6
.getPaint().setFlags(Paint
.UNDERLINE_TEXT_FLAG
);mTv6
.getPaint().setAntiAlias(true);mTv7
= findViewById(R
.id
.tv_7
);mTv7
.setText(Html
.fromHtml("<u>使用html中的下劃線</u>"));mTv8
= findViewById(R
.id
.tv_8
);mTv8
.setSelected(true);}
}
stings.xml
MainActivity.java
activity_text_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:padding="20dp"><TextViewandroid:id="@+id/tv_1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="@string/tv_test1"android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:ellipsize="end"android:maxLines="1"android:text="@string/tv_test1"android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"android:drawableRight="@drawable/pic1"android:drawablePadding="5dp"android:text="篩選"android:textColor="#3F51B5"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="這是一行文字,文字的中劃線在java中產生,并且使用setAntiAlias(true);成功刪除了鋸齒"android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_5"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="這是一行文字,文字的中劃線在java中產生,并且含有鋸齒"android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_6"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text="文字下劃線測試"android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_7"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:text=""android:textColor="#000000"android:textSize="20sp" /><TextViewandroid:id="@+id/tv_8"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="10dp"android:ellipsize="marquee"android:singleLine="true"android:text="我不想被人畫個圈圈成為有些人喜聞樂道的對象;我不想因為弄些子虛烏有的“新聞”被有些人熟知;我只想專注于我的工作、我的職業;我只想真誠的面對我的生活;我只想單純的熱愛我愛的一切。這條路我不急,急什么呢?爭搶?欺騙?我做不到也學不會。也不希望我身邊的人玩那些大家都懂的手段。也許讓有的人失望了,沒能成為你們想要看到的那一種星星。只想說我選擇了我喜歡的職業,前進的每一步都希望堅實。懂的不必解釋;不懂的,無須表達。我就是我,我還是我,我永遠是我。………今天表達了很多,其實一直在心里最想感謝的是無私付出的你們~希望我們可以一直默契的,默契的走下去!!"android:textColor="#000000"android:textSize="20dp"android:marqueeRepeatLimit="marquee_forever"android:focusable="true"android:focusableInTouchMode="true"/></LinearLayout>
總結
以上是生活随笔為你收集整理的【Android开发】我的第一个安卓程序的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。