国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Android應用開發(fā)(4)視圖布局基本屬性

這篇具有很好參考價值的文章主要介紹了Android應用開發(fā)(4)視圖布局基本屬性。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

Android應用開發(fā)學習筆記——目錄索引

本章介紹視圖(View)的基本概念及其用法,包括:如何設置視圖的寬度和高度,如何設置視圖的外部間距和內部間距,如何設置視圖的外部對齊方式和內部對齊方式等。

在Android中,什么是視圖(View)?View是Android中所有控件的基類,不管是簡單的TextView,Button還是復雜的LinearLayout和ListView,它們的共同基類都是View;

View是一種界面層的控件的一種抽象,它代表了一個控件,除了View還有ViewGroup,從名字來看ViewGroup可以翻譯為控件組,即一組View;

在Android中,ViewGroup也繼承了View,這就意味著View可以是單個控件,也可以是由多個控件組成的一組控件;

layout_marginhorizontal,Android應用開發(fā)學習筆記,android,android studio

View坐標對應圖

一、設置視圖的寬和高

Layout XML中的控件屬性:android:layout_width 和 android:layout_height

控件屬性

取值

說明

android:layout_width

match_parent

與上級視圖保持一致

wrap_content

與內容自適應

dp具體尺寸

以dp為單位的具體具體尺寸,如200dp

android:layout_height

match_parent

與上級視圖保持一致

wrap_content

與內容自適應

dp具體尺寸

已dp為單位的具體具體尺寸,如200dp

在Java代碼中設置視圖寬、高:

  1. XML中寬、高屬性需要取值wrap_content

  1. 調用控件對象的getLayoutParams()獲取布局參數,如TextView:


TextView textView = (TextView)findViewById(R.id.textView);
ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
  1. 修改布局參數width、height(單位為pixel)


layoutParams.width = dp2px(this, 200);
layoutParams.height = dp2px(this, 50);
int dp2px(Context context, float dp) {
    float density = context.getResources().getDisplayMetrics().density;
    return (int) (density * dp + 0.5f);
}
  1. 調用控件對象setLayoutParams(LayoutParams params)方法設置


textView.setLayoutParams(layoutParams);

二、設置視圖的間距

  1. layout_margin:當前視圖與外部視圖(包括上級視圖和平級視圖)之間的距離屬性。

  1. padding:當前視圖與內部視圖(包括下級視圖和內部文本)之間的距離屬性。

layout_margin

控件屬性

說明

android:layout_margin

一次性指定此視圖與外部視圖四周邊距,適用于所有視圖

android:layout_marginLeft

指定此視圖與外部視圖左邊距

android:layout_marginTop

指定此視圖與外部視圖上邊距

android:layout_marginRight

指定此視圖與外部視圖右邊距

android:layout_marginBottom

指定此視圖與外部視圖下邊距

android:layout_marginHorizontal

指定此視圖與外部視圖左側和右側的邊距

android:layout_marginVertical

指定此視圖與外部視圖上側和下側的邊距

android:layout_marginStart

效果同android:layout_marginLeft

android:layout_marginEnd

效果同android:layout_marginRight

padding:

控件屬性

說明

android:padding

一次性指定此視圖四周邊距,適用于所有視圖

android:paddingLeft

指定此視圖左邊距

android:paddingTop

指定此視圖上邊距

android:paddingRight

指定此視圖右邊距

android:paddingBottom

指定此視圖下邊距

android:paddingHorizontal

指定此視圖左側和右側的邊距

android:paddingVertical

指定此視圖上側和下側的邊距

android:paddingStart

指定此視圖左邊距,效果同android:paddingLeft

android:paddingEnd

指定此視圖右邊距,效果同android:paddingRight

不管是布局還是控件,都是有視圖基類View派生而來,layout_margin和padding是View的一個通用屬性。

<LinearLayout
    android:layout_width="300dp"
    android:layout_height="150dp"
    android:layout_marginTop="32dp"
    android:orientation="vertical"
    android:background="@color/gray">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="32dp"
        android:padding="32dp"
        android:background="@color/red"
        android:orientation="horizontal">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/blue" />
    </LinearLayout>
</LinearLayout>

顯示如下圖:

layout_marginhorizontal,Android應用開發(fā)學習筆記,android,android studio

三、設置視圖的對齊方式

參考:Android界面布局屬性layout_gravity和gravity的區(qū)別_Hard Coder的博客-CSDN博客

  1. android:layout_gravity:當前視圖與上級視圖(父容器)的對齊方式。

  1. android:gravity:設置子元素在該容器內的對齊方式。

android默認是左上對齊。

layout_gravity和gravity可以設置的值:top、bottom、left、right、center_vertical、fill_vertical、center_horizontal、fill_horizontal、center、fill、clip_vertical。(一個屬性可以包含多個值,需用 “|” 分開),其具體作用如下:

layout_marginhorizontal,Android應用開發(fā)學習筆記,android,android studio

控件屬性

取值

說明

android:layout_gravity

top

朝上對齊,將對象放在其容器的頂部,不改變其大小

bottom

朝下對齊,將對象放在其容器的底部,不改變其大小

left

靠左對齊,將對象放在其容器的左側,不改變其大小

right

靠右對齊,將對象放在其容器的右側,不改變其大小

center

將對象居中,不改變其大小

center_horizontal

將對象橫向居中,不改變其大小

水平對齊方式,水平方向居中對齊

center_vertical

將對象縱向居中,不改變其大小

垂直對齊方式,垂直方向居中對齊

fill

必要的時候增加對象的橫縱向大小,以完全充滿容器

fill_horizontal

必要的時候增加對象的橫向大小,以完全充滿容器

水平方向填充

fill_vertical

必要的時候增加對象的縱向大小,以完全充滿容器

垂直方向填充

clip_horizontal

附加選項

用于按照容器的邊來剪切對象的左側和/或右側的內容,剪切基于其橫向對齊設置;

左側對齊時,剪切右側;右側對齊時,剪切左側,除此之外剪切左側和右側;

水平方向裁剪

clip_vertical

附加選項

用于按照容器的邊來剪切對象的頂部和/或底部的內容,剪切基于其縱向對齊設置;

頂部對齊時,剪切底部;底部對齊時,剪切頂部,除此之外剪切頂部和底部;

垂直方向裁剪

<?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="450dp"
    android:background="@color/gray"
    android:padding="10dp">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:background="@color/white"
        android:layout_gravity="bottom"
        android:gravity="left"
        android:orientation="horizontal">
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/red"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="200dp"
        android:layout_weight="1"
        android:layout_gravity="top"
        android:gravity="right"
        android:background="@color/white"
        android:orientation="horizontal">
        <View
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@color/red"/>
    </LinearLayout>
</LinearLayout>

顯示如下圖:

layout_marginhorizontal,Android應用開發(fā)學習筆記,android,android studio

四、常見控件的基本屬性總結

TextView:

向用戶顯示文本的用戶界面元素

public class TextView?extends?View?implements?ViewTreeObserver.OnPreDrawListener

XML中屬性 描述
android:id 組件id
android:layout_width 組件寬度
android:lauout_height 組件高度
android:text 文本內容
android:textSize 文本大小
android:textColor 文本顏色
android:background 組件背景
android:ellipsize

當文字長度超過textview寬度時的省略顯示方式

"start"省略號顯示在開頭

"end"省略號顯示在結尾

"middle"省略號顯示在中間

"marquee"以橫向滾動方向顯示(需要獲取當前焦點)

android:layout_weight 權重占比
設置間距

android:layout_margin

一次性指定此視圖與外部視圖四周邊距,適用于所有視圖

android:layout_marginLeft

指定此視圖與外部視圖左邊距

android:layout_marginTop

指定此視圖與外部視圖上邊距

android:layout_marginRight

指定此視圖與外部視圖右邊距

android:layout_marginBottom

指定此視圖與外部視圖下邊距

android:layout_marginHorizontal

指定此視圖與外部視圖左側和右側的邊距

android:layout_marginVertical

指定此視圖與外部視圖上側和下側的邊距

android:layout_marginStart

效果同android:layout_marginLeft

android:layout_marginEnd

效果同android:layout_marginRight

android:padding

一次性指定此視圖四周邊距,適用于所有視圖

android:paddingLeft

指定此視圖左邊距

android:paddingTop

指定此視圖上邊距

android:paddingRight

指定此視圖右邊距

android:paddingBottom

指定此視圖下邊距

android:paddingHorizontal

指定此視圖左側和右側的邊距

android:paddingVertical

指定此視圖上側和下側的邊距

android:paddingStart

指定此視圖左邊距,效果同android:paddingLeft

android:paddingEnd

指定此視圖右邊距,效果同android:paddingRight

對齊方式
android:layout_gravity 當前視圖與上級視圖(父容器)的對齊方式
android:gravity 設置子元素在該容器內的對齊方式
android:textStyle 文本的樣式(正常、粗體、斜體、粗體|斜體)
android:layout_gravity 當前視圖與上級視圖(父容器)的對齊方式
android:gravity 設置子元素在該容器內的對齊方式
android:textStyle 文本的樣式(正常、粗體、斜體、粗體|斜體)
android:typeface 文本行之間的顯式高度
... ...

ImageView:

public class ImageView?extends?View

XML中屬性 描述

android:adjustViewBounds

如果您希望 ImageView 調整其邊界以保持其可繪制對象的縱橫比,請將此設置為 true。

android:baseline

此視圖中基線的偏移量。

android:baselineAlignBottom

如果為真,則圖像視圖將基于其底部邊緣進行基線對齊。

android:cropToPadding

如果為真,圖像將被裁剪以適合其填充。

android:maxHeight

一個可選參數,用于為此視圖提供最大高度。

android:maxWidth

一個可選參數,用于為此視圖提供最大寬度。

android:scaleType

控制應如何調整圖像大小或移動圖像以匹配此 ImageView 的大小。

android:src

將可繪制對象設置為此 ImageView 的內容。

android:tint

圖像的著色顏色。

android:tintMode

用于應用圖像色調的混合模式。

EditText:

public class EditText?extends?TextView

XML中屬性 描述
android:textCursorDrawable 設置光標顏色
android:textColorHint 輸入框提示字體顏色
android:inputType 輸入內容類型
android:text 文本內容
android:enableTextStylingShortcuts 啟用樣式快捷方式

五、測試程序

java:

MainActivity.java


package com.example.viewattributestest;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.os.TestLooperManager;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private final static String TAG = "lzl-test-ViewAttributes";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView) findViewById(R.id.textView);
        ViewGroup.LayoutParams layoutParams = textView.getLayoutParams();
        Log.i(TAG, "onCreate: layoutParams.width:" + layoutParams.width + ", layoutParams.height:" + layoutParams.height);
        textView.setBackgroundColor(Color.GREEN);
        textView.setText("setLayoutParams測試");
        layoutParams.width = dp2px(this, 200);
        layoutParams.height = dp2px(this, 50);
        textView.setLayoutParams(layoutParams);
        textView.setText("setLayoutParams:width:" + 200 + ", height:" + 50 + "(dp)");
    }

    private int dp2px(Context context, float dp) {
        float density = context.getResources().getDisplayMetrics().density;
        return (int) (density * dp + 0.5f);
    }
}

xml:

res/layout/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:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="300dp"
        android:layout_height="150dp"
        android:background="@color/gray"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="32dp"
            android:background="@color/red"
            android:orientation="horizontal"
            android:padding="32dp">

            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/blue" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="450dp"
        android:layout_marginTop="10dp"
        android:background="@color/gray"
        android:padding="10dp"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout"
        tools:layout_editor_absoluteX="-16dp">

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="200dp"
            android:layout_gravity="bottom"
            android:layout_weight="1"
            android:background="@color/white"
            android:gravity="left"
            android:orientation="horizontal">

            <View
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@color/red" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="200dp"
            android:layout_gravity="top"
            android:layout_weight="1"
            android:background="@color/white"
            android:gravity="right"
            android:orientation="horizontal">

            <View
                android:layout_width="100dp"
                android:layout_height="100dp"
                android:background="@color/red" />
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
</LinearLayout>

res/values/colors.xml中添加

    <color name="white">#FFFFFFFF</color>
    <color name="red">#FFFF0000</color>
    <color name="green">#FF00FF00</color>
    <color name="blue">#FF0000FF</color>
    <color name="gray">#FF888888</color>

模擬器上運行

layout_marginhorizontal,Android應用開發(fā)學習筆記,android,android studio?

源碼及測試apk

百度網盤鏈接:百度網盤 請輸入提取碼 提取碼:test

github下載地址:

GitHub - liuzhengliang1102/AndroidStudio-LearnAppDevelopment

android:enableTextStylingShortcuts文章來源地址http://www.zghlxwxcb.cn/news/detail-768333.html

到了這里,關于Android應用開發(fā)(4)視圖布局基本屬性的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • HarmonyOS應用開發(fā)學習筆記 UI布局學習 柵格布局(GridRow/GridCol)

    HarmonyOS應用開發(fā)學習筆記 UI布局學習 柵格布局(GridRow/GridCol)

    HarmonyOS應用開發(fā)學習筆記 UI布局學習 相對布局 (RelativeContainer) 官方文檔:柵格布局(GridRow/GridCol) 通過設置GridRow的direction屬性來指定柵格子組件在柵格容器中的排列方向 代碼 描述 GridRowDirection.Row 從左往右排列 GridRowDirection.RowReverse 從右往左排列 左往右排列 子組件從右

    2024年02月03日
    瀏覽(24)
  • HarmonyOS應用開發(fā)學習筆記 ArkTS 布局概述

    HarmonyOS應用開發(fā)學習筆記 ArkTS 布局概述

    布局指用特定的組件或者屬性來管理用戶頁面所放置UI組件的大小和位置。在實際的開發(fā)過程中,需要遵守以下流程保證整體的布局效果 確定頁面的布局結構。 分析頁面中的元素構成。 選用適合的布局容器組件或屬性控制頁面中各個元素的位置和大小約束。 布局元素的組成

    2024年02月01日
    瀏覽(28)
  • Python Flask框架-開發(fā)簡單博客-項目布局、應用設置

    Python Flask框架-開發(fā)簡單博客-項目布局、應用設置

    作者:Eason_LYC 悲觀者預言失敗,十言九中。 樂觀者創(chuàng)造奇跡,一次即可。 一個人的價值,只在于他所擁有的。所以可以不學無術,但不能一無所有! 技術領域:WEB安全、網絡攻防 關注WEB安全、網絡攻防。我的專欄文章知識點全面細致,邏輯清晰、結合實戰(zhàn),讓你在學習路

    2024年02月02日
    瀏覽(52)
  • 鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:組件標識)

    鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:組件標識)

    id為組件的唯一標識,在整個應用內唯一。本模塊提供組件標識相關接口,可以獲取指定id組件的屬性,也提供向指定id組件發(fā)送事件的功能。 說明: 從API Version 8開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。 名稱 參數說明 描述 id string 組件

    2024年04月22日
    瀏覽(40)
  • 鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:文本通用)

    鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:文本通用)

    文本通用屬性目前只針對包含文本元素的組件,設置文本樣式。 說明: 從API Version 7開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。 名稱 參數類型 描述 fontColor ResourceColor 設置字體顏色。 從API version 9開始,該接口支持在ArkTS卡片中使用。 fon

    2024年03月23日
    瀏覽(22)
  • 鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:安全區(qū)域)

    鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:安全區(qū)域)

    安全區(qū)域是指頁面的顯示區(qū)域,默認不與系統(tǒng)設置的非安全區(qū)域比如狀態(tài)欄、導航欄區(qū)域重疊,默認情況下開發(fā)者開發(fā)的界面都被布局在安全區(qū)域內。提供屬性方法允許開發(fā)者設置組件繪制內容突破安全區(qū)域的限制,通過expandSafeArea屬性支持組件不改變布局情況下擴展其繪制

    2024年04月29日
    瀏覽(15)
  • 鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:尺寸設置)

    鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:尺寸設置)

    用于設置組件的寬高、邊距。 說明: 從API Version 7開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。 width(value: Length) 設置組件自身的寬度,缺省時使用元素自身內容需要的寬度。若子組件的寬大于父組件的寬,則會畫出父組件的范圍。 從API ver

    2024年03月15日
    瀏覽(18)
  • 鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:邊框設置)

    鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:邊框設置)

    設置組件邊框樣式。 說明: 從API Version 7開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。 border(value: BorderOptions) 設置邊框樣式。 卡片能力: ?從API version 9開始,該接口支持在ArkTS卡片中使用。 系統(tǒng)能力: ?SystemCapability.ArkUI.ArkUI.Full 參數: 參數

    2024年04月23日
    瀏覽(26)
  • 鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:組件內容模糊)

    鴻蒙Harmony應用開發(fā)—ArkTS聲明式開發(fā)(通用屬性:組件內容模糊)

    為當前組件添加內容模糊效果。 說明: 從API Version 10開始支持。后續(xù)版本如有新增內容,則采用上角標單獨標記該內容的起始版本。 foregroundBlurStyle(value: BlurStyle, options?: ForegroundBlurStyleOptions) 為當前組件提供內容模糊能力。 系統(tǒng)能力: ?SystemCapability.ArkUI.ArkUI.Full 參數: 參數

    2024年03月09日
    瀏覽(25)
  • 【HarmonyOS】【應用開發(fā)】動畫-屬性動畫與顯式動畫

    (一)、定義 ??官網對于屬性動畫的定義如下: 組件的某些通用屬性變化時,可以通過屬性動畫實現漸變過渡效果,提升用戶體驗。支持的屬性包括width、height、backgroundColor、opacity、scale、rotate、translate等 ??個人理解:屬性動畫針對的是同一個組件的屬性變化情況,如

    2024年02月02日
    瀏覽(16)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包