如何将安卓垂直布局更改为水平布局?

huangapple go评论60阅读模式
英文:

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&#39;m very new to android and I&#39;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&#39;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">

            &lt;LinearLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:gravity=&quot;center&quot;
android:orientation=&quot;vertical&quot;
android:paddingTop=&quot;@dimen/_10sdp&quot;&gt;
&lt;androidx.cardview.widget.CardView
android:layout_width=&quot;@dimen/_230sdp&quot;
android:layout_height=&quot;@dimen/_50sdp&quot;
android:layout_marginTop=&quot;@dimen/_10sdp&quot;
android:gravity=&quot;center&quot;
app:cardBackgroundColor=&quot;@color/home_btn_bg_color&quot;
app:cardCornerRadius=&quot;@dimen/_8sdp&quot;
app:cardElevation=&quot;2dp&quot;&gt;
&lt;RelativeLayout
android:id=&quot;@+id/wsBtn&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;&gt;
&lt;ImageView
android:layout_width=&quot;@dimen/_30sdp&quot;
android:padding=&quot;@dimen/_6sdp&quot;
android:layout_height=&quot;@dimen/_30sdp&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_marginLeft=&quot;@dimen/_12sdp&quot;
android:background=&quot;@drawable/ic_icon_bg&quot;
android:src=&quot;@drawable/whts&quot; /&gt;
&lt;ImageView
android:layout_width=&quot;@dimen/_12sdp&quot;
android:layout_height=&quot;@dimen/_12sdp&quot;
android:layout_alignParentRight=&quot;true&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_marginRight=&quot;@dimen/_15sdp&quot;
android:src=&quot;@drawable/right_arrow&quot;
app:tint=&quot;@color/home_right_arrow_color&quot; /&gt;
&lt;TextView
android:id=&quot;@+id/txt1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_marginLeft=&quot;@dimen/_60sdp&quot;
android:text=&quot;@string/whatsapp&quot;
android:textColor=&quot;@color/home_btn_txt&quot;
android:textSize=&quot;@dimen/_13sdp&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;/RelativeLayout&gt;
&lt;/androidx.cardview.widget.CardView&gt;
&lt;/LinearLayout&gt;
&lt;/ScrollView&gt;
&lt;/LinearLayout&gt;

[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=&quot;horizontal&quot;
</details>

huangapple
  • 本文由 发表于 2023年2月10日 16:07:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定