【问题标题】:How to Display list-view items in horizontal Android如何在水平 Android 中显示列表视图项目
【发布时间】:2017-10-01 18:15:18
【问题描述】:

我正在处理列表视图..列表视图项目以垂直方式显示。但我以水平方式显示项目但不以水平方式显示项目。 并且 simple_list_item_1 不起作用..我正在向左或向右改变重力,但项目垂直显示..这是我的代码

这里是 content_main

<RelativeLayout 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"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal">



        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/editText"
            android:layout_centerHorizontal="true" />
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Searach Now"
            android:id="@+id/button"
            android:layout_alignParentTop="true"
            android:layout_toLeftOf="@+id/editText" />
    </LinearLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">



        <ListView
            android:id="@+id/listView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"

            android:layout_below="@+id/headingText">


        </ListView>

    </LinearLayout>

</RelativeLayout>

这里是 simple_list_item。 XML

 <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="left"></TextView>

    <TextView
        android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="right"
       ></TextView>

</Relativelayout>

【问题讨论】:

    标签: android listview arraylist


    【解决方案1】:

    我知道您是初学者,recyclerView 为此提供了 LayoutManagers,但由于您是新手,因此了解 listview 是很好的,因为它是基本概念并且比 recyclerview 更易于设置。

    使用水平滚动视图并将列表视图嵌入其中。这样做不是将列表项垂直放置,而是使用滚动选项以水平方式放置。

    【讨论】:

      【解决方案2】:

      必须使用方式:根据 Android 文档RecyclerView 是在列表视图中组织项目并水平显示的最佳方式

      优点:

      1. 由于使用 Recyclerview 适配器,ViewHolder pattern 是 自动实现
      2. 动画易于执行
      3. 更多功能

      示例:

      survivingwithandroid.com

      只需添加下面的块,使ListView 从垂直变为水平

      代码-sn-p

      LinearLayoutManager layoutManager= new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false);
      mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
      mRecyclerView.setLayoutManager(layoutManager);
      

      参考:Horizontal ListView in Android?

      另一种方式:

      <RelativeLayout 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"
          tools:context=".MainActivity">
      
      <HorizontalScrollView
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:scrollbars="horizontal">
      
          <LinearLayout
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal">
      
              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_margin="20dp"
                  android:background="#f00"
                  android:padding="10dp"
                  android:text="Button 1"
                  android:textColor="#fff"
                  android:textSize="20sp" />
      
              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_margin="20dp"
                  android:background="#0f0"
                  android:padding="10dp"
                  android:text="Button 2"
                  android:textColor="#fff"
                  android:textSize="20sp" />
      
              <Button
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center"
                  android:layout_margin="20dp"
                  android:background="#00f"
                  android:padding="10dp"
                  android:text="Button 3"
                  android:textColor="#fff"
                  android:textSize="20sp" />
      
          </LinearLayout>
      
      </HorizontalScrollView>
      
      </RelativeLayout>
      

      结果代码:

      HorizontalScrollView 的问题是显示水平条,不建议使用。

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 2014-09-12
        • 1970-01-01
        • 2010-12-01
        • 1970-01-01
        • 2020-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多