[英]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.