[英]How to align buttons side by side in scroll view?
我刚开始使用android studio ..我试图将ImageButtons并排放在滚动视图中,但我不能将它们并排放置,我也不能从左侧调整它们。 我如何并排排列?
我已经尝试了其中一个答案给出的解决方案: 在线性布局内部ScrollView内部并排对齐TextView但它不起作用
<?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=".StoryIndex">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageButton
android:id="@+id/titlee"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
app:srcCompat="@drawable/alami1" />
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="375dp"
android:layout_height="116dp"
android:background="@android:color/black"
app:srcCompat="@drawable/alami2" />
<ImageButton
android:id="@+id/imageButton9"
android:layout_width="195dp"
android:layout_height="169dp"
app:srcCompat="@drawable/alami3" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
我希望图像按钮并排,但它们是成排的,彼此叠加..但它显示的像图片中的那个:设计视图的图像谢谢。
您必须将LinearLayout中的方向从垂直更改为水平。
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
尝试这个
<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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
//The question you mentioned can be obtained by using weightSum
android:weightSum="15">
//replace src ic_launcher_background by your drawable source.
<ImageButton
android:id="@+id/titlee"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:src="@drawable/ic_launcher_background" />
<ImageButton
android:id="@+id/imageButton8"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:src="@drawable/ic_launcher_background" />
<ImageButton
android:id="@+id/imageButton9"
android:layout_width="0dp"
android:layout_weight="5"
android:background="@android:color/black"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
试试这个,让我知道。 快乐的编码。
<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">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="15">
<ImageButton
android:id="@+id/imageButtonOne"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:src="@drawable/ic_launcher_background" />
<ImageButton
android:id="@+id/imageButtonTwo"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:src="@drawable/ic_launcher_background" />
<ImageButton
android:id="@+id/imageButtonThree"
android:layout_width="0dp"
android:layout_weight="5"
android:background="@android:color/black"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher_background"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
如果您仍然发现任何问题,请尝试此操作,请告知我们。
将图像按钮显示为并排,并按行显示。
将LinearLayout
的方向从vertical
更改为horizontal
。
请尝试以下代码:
<?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=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:id="@+id/titlee"
android:layout_weight=".3"
android:layout_width="0dp"
android:layout_height="116dp"
android:background="@android:color/black"
app:srcCompat="@drawable/stars_one"
android:layout_margin="4dp"/>
<ImageButton
android:layout_weight=".4"
android:id="@+id/imageButton8"
android:layout_width="0dp"
android:layout_height="116dp"
android:background="@android:color/black"
app:srcCompat="@drawable/stars_one"
android:layout_margin="4dp"/>
<ImageButton
android:layout_weight=".3"
android:id="@+id/imageButton9"
android:layout_width="0dp"
android:layout_height="116dp"
app:srcCompat="@drawable/stars_one"
android:layout_margin="4dp"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
上面代码的输出如下:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.