[英]Android activity design with layouts
I have to create Android activity which looks like this: 我必须创建如下所示的Android活动:
Now I've created only such part of the activity. 现在,我仅创建了活动的这一部分。
Which coded this way. 哪个用这种方式编码。
<?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>
Please help me how to put last four buttons to the activity. 请帮助我如何在活动中放置最后四个按钮。
The particular question is: 特定的问题是:
How to put four buttons in the center of the screen. 如何在屏幕中央放置四个按钮。
I am trying to put a sample code for you. 我正在尝试为您提供示例代码。 This code can give you idea how to place four button at center of activity. 该代码可以使您了解如何在活动中心放置四个按钮。
<?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>
Just use this on the top Linear Layout 只需在顶部线性布局上使用
gravity="center"
OR.... 要么....
<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" />
If this doesnt work try to do this : Nest 2 ImageViews inside 1 LinearLayout and create 2 Linear Layouts with weight = 1 如果这样做不起作用,请尝试执行以下操作:将2个ImageView嵌套在1个LinearLayout中,并创建2个权重= 1的线性布局
Code : 代码:
<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>
Inside your vertical linear layout you can give 2 more linear layouts with horizontal orientation and also with weightsum =2. 在您的垂直线性布局中,您可以给出2个水平方向的线性布局,并且权重和= 2。 Inside both these two linear layouts add 2 more linear layouts with weight =1.Give apropriate paddings for peoper button looks and then you can give the click action to the layouts. 在这两个线性布局中都添加了两个权重为= 1的线性布局。为peoper按钮外观提供适当的填充,然后可以对布局进行单击操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.