Android學習記錄(四)
- 線性布局
- 網格布局
- 幀式布局
- 幀式布局常用屬性
- 案例演示
- 效果展示
- 代碼展示
今天的學習內容為Android中的常用布局,在前幾天的任務中,我已經使用過了線性布局以及網格布局,所以今天我首先會進行兩種布局方法的進一步了解,然后在進行新內容的學習,
線性布局

網格布局

幀式布局
幀式布局常用屬性
android:foreground:*設定改幀布局容器的前景影像
android:foregroundGravity:設定前景影像顯示的位置
android:scrollbars:滾動條(none、horizontal、vertical)
android:layout_marginTop:上邊距
android:layout_marginBottom:下邊距
android:layout_marginLeft:左邊距
android:layout_marginRight:右邊距
android:paddingLeft:左內邊距
android:paddingRight:右內邊距
android:paddingTop:上內邊距
android:paddingBottom:下內邊距
android:background:背景
案例演示
效果展示

代碼展示
1.activity_main.xml檔案
<?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"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvBottom"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_gravity="center"
android:background="#ff0000"
android:text="@string/bottom"
android:textColor="#ffff00"
android:textSize="30sp"/>
<TextView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/tvMiddle"
android:layout_gravity="center"
android:background="#0000ff"
android:text="@string/middle"
android:textColor="#ffff00"
android:textSize="30sp"/>
<TextView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/tvTop"
android:layout_gravity="center"
android:background="#00ff00"
android:text="@string/top"
android:textColor="#ffff00"
android:textSize="30sp"/>
</FrameLayout>
<Button
android:id="@+id/btnSwitchColor"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:onClick="doSwitchColor"
android:text="@string/switch_color"
android:textSize="20sp" />
</LinearLayout>
2.strings.xml檔案
<resources>
<string name="app_name">SwitchColor</string>
<string name="bottom">底層</string>
<string name="middle">中層</string>
<string name="top">頂層</string>
<string name="switch_color">切換顏色</string>
</resources>
3.MainActivity.java檔案
package net.nell.switchcolor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvBottom;
private TextView tvMiddle;
private TextView tvTop;
private int clickCount;
private int[] colors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通過資源標識獲得控制元件實體
tvBottom = findViewById(R.id.tvBottom);
tvMiddle = findViewById(R.id.tvMiddle);
tvTop = findViewById(R.id.tvTop);
}
/**
* 切換顏色單擊事件處理方法
*
* @param view
*/
public void doSwitchColor(View view){
//累計按鈕單擊次數
clickCount++;
//單擊次數對3求余
clickCount = clickCount % 3;
//判斷次數是0、1、2
switch (clickCount){
case 0 :
//紅——藍——綠
colors = new int[]{Color.RED, Color.BLUE,Color.GREEN};
break;
case 1:
//藍——綠——紅
colors = new int[]{Color.BLUE,Color.GREEN,Color.RED};
break;
case 2:
//綠——紅——藍
colors = new int[]{Color.GREEN,Color.RED,Color.BLUE};
break;
}
//根據切換后的顏色陣列來設定三層標簽顏色
tvBottom.setBackgroundColor(colors[0]);
tvMiddle.setBackgroundColor(colors[1]);
tvTop.setBackgroundColor(colors[2]);
}
}
為達到同樣的效果,我們可以進行代碼的優化,具體如下所示:
package net.nell.switchcolor;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private TextView tvBottom;
private TextView tvMiddle;
private TextView tvTop;
//private int clickCount;
private int[] colors;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//通過資源標識獲得控制元件實體
tvBottom = findViewById(R.id.tvBottom);
tvMiddle = findViewById(R.id.tvMiddle);
tvTop = findViewById(R.id.tvTop);
//初始化顏色陣列
colors = new int[]{Color.RED,Color.BLUE,Color.GREEN};
}
/**
* 切換顏色單擊事件處理方法
*
* @param view
*/
public void doSwitchColor(View view){
/**
//累計按鈕單擊次數
clickCount++;
//單擊次數對3求余
clickCount = clickCount % 3;
//判斷次數是0、1、2
switch (clickCount){
case 0 :
//紅——藍——綠
colors = new int[]{Color.RED, Color.BLUE,Color.GREEN};
break;
case 1:
//藍——綠——紅
colors = new int[]{Color.BLUE,Color.GREEN,Color.RED};
break;
case 2:
//綠——紅——藍
colors = new int[]{Color.GREEN,Color.RED,Color.BLUE};
break;*/
//切換顏色
int temp = colors[0];
colors[0] = colors[1];
colors[1] = colors[2];
colors[2] = temp;
/**
* int temp = colors[0];
* for(int i = 0;i < colors.length - 1;i++){
* colors[i] = colors[i + 1];
* }
* colors[colors.length -1] = temp;
*/
//設定三層標簽的顏色
tvBottom.setBackgroundColor(colors[0]);
tvMiddle.setBackgroundColor(colors[1]);
tvTop.setBackgroundColor(colors[2]);
}
/**
//根據切換后的顏色陣列來設定三層標簽顏色
tvBottom.setBackgroundColor(colors[0]);
tvMiddle.setBackgroundColor(colors[1]);
tvTop.setBackgroundColor(colors[2]); *
*/
}
其中注釋部分代表不同的方法,
以上就是我今天的學習內容,雖然不是很多,但是我的識訓很大,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/247586.html
標籤:其他
