英文:
How to change andriod vertical items layout to horizonatly?
问题
我很新于Android,并且正在自己从互联网上学习Android。我想要动态的水平布局,类似于在CSS弹性布局中使用的项。我想要类似于这样的东西。
我尝试了一些代码,但结果类似于这样。
我尝试了以下代码:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.72"
android:background="@drawable/main_bg_home">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="@dimen/_10sdp">
<androidx.cardview.widget.CardView
android:layout_width="@dimen/_230sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginTop="@dimen/_10sdp"
android:gravity="center"
app:cardBackgroundColor="@color/home_btn_bg_color"
app:cardCornerRadius="@dimen/_8sdp"
app:cardElevation="2dp">
<RelativeLayout
android:id="@+id/wsBtn"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="@dimen/_30sdp"
android:padding="@dimen/_6sdp"
android:layout_height="@dimen/_30sdp"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/_12sdp"
android:background="@drawable/ic_icon_bg"
android:src="@drawable/whts" />
<ImageView
android:layout_width="@dimen/_12sdp"
android:layout_height="@dimen/_12sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/_15sdp"
android:src="@drawable/right_arrow"
app:tint="@color/home_right_arrow_color" />
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/_60sdp"
android:text="@string/whatsapp"
android:textColor="@color/home_btn_txt"
android:textSize="@dimen/_13sdp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
</LinearLayout>
<details>
<summary>英文:</summary>
I'm very new to android and I'm learning android from the internet by myself I want dynamic horizontal layouts. I want items as we use in css flex layouts. I want something like this
[![enter image description here][1]][1]
I tried some code with that code I'm getting the result something like this
[![enter image description here][2]][2]
I tried this code
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.72"
android:background="@drawable/main_bg_home">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:paddingTop="@dimen/_10sdp">
<androidx.cardview.widget.CardView
android:layout_width="@dimen/_230sdp"
android:layout_height="@dimen/_50sdp"
android:layout_marginTop="@dimen/_10sdp"
android:gravity="center"
app:cardBackgroundColor="@color/home_btn_bg_color"
app:cardCornerRadius="@dimen/_8sdp"
app:cardElevation="2dp">
<RelativeLayout
android:id="@+id/wsBtn"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="@dimen/_30sdp"
android:padding="@dimen/_6sdp"
android:layout_height="@dimen/_30sdp"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/_12sdp"
android:background="@drawable/ic_icon_bg"
android:src="@drawable/whts" />
<ImageView
android:layout_width="@dimen/_12sdp"
android:layout_height="@dimen/_12sdp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="@dimen/_15sdp"
android:src="@drawable/right_arrow"
app:tint="@color/home_right_arrow_color" />
<TextView
android:id="@+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/_60sdp"
android:text="@string/whatsapp"
android:textColor="@color/home_btn_txt"
android:textSize="@dimen/_13sdp"
android:textStyle="bold" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</ScrollView>
</LinearLayout>
[1]: https://i.stack.imgur.com/GlJSk.jpg
[2]: https://i.stack.imgur.com/qFAFL.jpg
</details>
# 答案1
**得分**: 2
对于您的情况,最好使用[RecyclerView][1]。您可以在这里看到一个[示例][2],演示了如何实现相同的功能。
而对于网格,您可以使用`RecyclerView.GridLayoutManager(context, 2)`。
对于垂直列表,您可以使用`RecyclerView.LinearLayoutManager(context)`。
对于水平列表,您可以使用`RecyclerView.LineaLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)`。
希望这对您有所帮助:)
[1]: https://developer.android.com/develop/ui/views/layout/recyclerview
[2]: https://www.geeksforgeeks.org/android-recyclerview/
<details>
<summary>英文:</summary>
For your case, better to use [RecyclerView][1]. You can see here a [example][2] that how to implement the same.
And for Grid you can use `RecyclerView.GridLayoutManager(context, 2)`.
And for Vertical List you can use `RecyclerView.LinearLayoutManager(context)`.
And for Horizontal List you can use `RecyclerView.LineaLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)`.
Hope this will help you:)
[1]: https://developer.android.com/develop/ui/views/layout/recyclerview
[2]: https://www.geeksforgeeks.org/android-recyclerview/
</details>
# 答案2
**得分**: 0
请将您的 **LinearLayout 方向更改为水平**,如下所示:
```android:orientation="horizontal"```
<details>
<summary>英文:</summary>
Please update your **LinearLayout orientation to horizontal**
look like this
android:orientation="horizontal"
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论