一、com.android.support:design
1、com.android.support:design 是 Android 官方提供的一個用於 Material Design 風格的 UI 控制項庫。通常使用在 Android App 開發中,可以幫助開發者快速、簡單的實現 Material Design 風格的界面。下面是一個實現 Toolbar 和 Navigation Drawer 的示例代碼:
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/nav_menu" />
</android.support.v4.widget.DrawerLayout>
Java 代碼:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, mDrawerLayout, mToolbar, R.string.app_name, R.string.app_name);
mDrawerLayout.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the home action
} else if (id == R.id.nav_messages) {
// Handle the messages action
} else if (id == R.id.nav_friends) {
// Handle the friends action
}
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
}
2、以上代碼實現了 Toolbar 和 Navigation Drawer 控制項的顯示,通過調用 ActionBarDrawerToggle 類的 syncState() 方法可以使 Toolbar 和 Navigation Drawer 並聯使用。此外,NavigationView 控制項的 setNavigationItemSelectedListener() 方法用於響應 NavigationView 中菜單項的選擇事件。
二、com.android.support:percent
1、com.android.support:percent 是 Android 官方提供的一個用於百分比布局的 UI 控制項庫。它是基於 RelativeLayout 布局實現的,可以讓開發者使用百分比的方式來布局控制項。下面是一個通過百分比布局控制 ImageView 大小的示例代碼:
XML 文件:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_heightPercent="50%"
app:layout_marginStartPercent="25%"
app:layout_marginTopPercent="25%"
app:layout_widthPercent="50%"
android:src="@drawable/image" />
</RelativeLayout>
2、以上代碼中,通過使用 app:layout_heightPercent、app:layout_marginStartPercent、app:layout_marginTopPercent、app:layout_widthPercent 屬性控制 ImageView 的大小和位置。這裡 app:layout_heightPercent、app:layout_widthPercent 屬性的值為 “50%”,表示 ImageView 的高和寬整體都是相對於父布局的一半。而 app:layout_marginStartPercent、app:layout_marginTopPercent 屬性的值為 “25%”,則表示 ImageView 的左偏移量和上偏移量都是相對於父布局的一4分之一。
三、com.android.support:appcompat-v7
1、com.android.support:appcompat-v7 是 Android 官方提供的一個用於支持舊版 Android 系統的 UI 控制項庫。通常使用在兼容性開發中,可以保證 App 在不同版本的 Android 系統上都能正常運行。下面是一個實現 Toolbar 的示例代碼:
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="My Toolbar" />
Java 代碼:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
}
2、以上代碼實現了 Toolbar 控制項的顯示,並分別在 XML 和 Java 中設置了其大小、背景色和標題。此外,在 Java 中調用 setSupportActionBar() 方法可以讓 Toolbar 接管 Activity 的 ActionBar。
四、com.android.support.constraint
1、com.android.support.constraint 是 Android 官方提供的一個用於約束布局(ConstraintLayout)的 UI 控制項庫。引入它可以在布局中使用 ConstraintLayout,進行複雜和響應式布局的開發,大大提高了 UI 布局的靈活性和可讀性。下面是一個使用 ConstraintLayout 實現登錄界面的示例代碼:
XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/logo"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/verticalGuideline" />
<android.support.constraint.Guideline
android:id="@+id/verticalGuideline"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.5" />
<EditText
android:id="@+id/usernameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Username"
android:inputType="text"
app:layout_constraintTop_toBottomOf="@id/verticalGuideline"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintEnd_toEndOf="@id/guideline" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintGuide_percent="0.35" />
<EditText
android:id="@+id/passwordEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintTop_toBottomOf="@id/usernameEditText"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintEnd_toEndOf="@id/guideline" />
<Button
android:id="@+id/loginButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Login"
app:layout_constraintTop_toBottomOf="@id/passwordEditText"
app:layout_constraintStart_toStartOf="@id/guideline"
app:layout_constraintEnd_toEndOf="@id/guideline" />
</android.support.constraint.ConstraintLayout>
2、以上代碼使用 ConstraintLayout 實現了登錄界面,通過設置 Guideline 控制項的位置和百分比,實現了 ImageView、EditText 和 Button 的位置控制。此外,ConstraintLayout 還可以通過設置約束比例、權重、尺寸等屬性來控制 UI 控制項的位置和大小,具有更高的靈活性和可擴展性。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/237273.html