[英]Android activity design with layouts
我必須創建如下所示的Android活動:
現在,我僅創建了活動的這一部分。
哪個用這種方式編碼。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="ru.alexeyzhulin.elecomp.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/main_icon" />
<SearchView
android:id="@+id/searchText"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:queryHint="@string/search_message"
android:textAlignment="center" />
<ImageButton
android:id="@+id/cartButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/cart_icon1" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
請幫助我如何在活動中放置最后四個按鈕。
特定的問題是:
如何在屏幕中央放置四個按鈕。
我正在嘗試為您提供示例代碼。 該代碼可以使您了解如何在活動中心放置四個按鈕。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" //I changed it to wrap_content
android:orientation="horizontal">
<ImageButton
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
/>
<SearchView
android:id="@+id/searchText"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:queryHint="assss"
android:textAlignment="center" />
<ImageButton
android:id="@+id/cartButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
/>
</LinearLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true">
<Button
android:id="@+id/b1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="button" />
<Button
android:id="@+id/b2"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:text="button" />
<Button
android:id="@+id/b3"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_below="@+id/b1"
android:layout_marginTop="40dp"
android:text="button" />
<Button
android:id="@+id/b4"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/b2"
android:layout_marginTop="40dp"
android:text="button" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
只需在頂部線性布局上使用
gravity="center"
要么....
<ImageButton
android:layout_weight = "1"
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/main_icon" />
如果這樣做不起作用,請嘗試執行以下操作:將2個ImageView嵌套在1個LinearLayout中,並創建2個權重= 1的線性布局
代碼:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/logoButton"
android:layout_width="55dp"
android:layout_weight = "1"
android:layout_height="55dp"
android:layout_weight="0"
android:background="@color/colorPrimary"
app:srcCompat="@drawable/main_icon" />
<SearchView
android:layout_weight = "1"
android:id="@+id/searchText"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_weight="1"
android:background="@color/colorPrimary"
android:queryHint="@string/search_message"
android:textAlignment="center" />
</LinearLayout>
在您的垂直線性布局中,您可以給出2個水平方向的線性布局,並且權重和= 2。 在這兩個線性布局中都添加了兩個權重為= 1的線性布局。為peoper按鈕外觀提供適當的填充,然后可以對布局進行單擊操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.