我需要在我的应用程序中创建表布局。但是,该表包含许多不能分开的列。因此,我想将表格布局放在水平scrollview中。
这是我的xml代码。main_activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent_layout">
<LinearLayout
android:id="@+id/linearid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFA500"
android:gravity="center"
android:paddingVertical="10dp"
android:text="@string/send"
android:textColor="#ffffff"
android:textSize="30sp"
android:textStyle="bold" />
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/myRecycle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_alignLeft="@+id/linearid"
android:layout_marginHorizontal="20dp"
android:layout_marginLeft="30dp" />
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
由表布局组成的Recyclerview xml
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:measureAllChildren="false"
android:scrollbars="none"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/border"
android:layout_width="match_parent"
android:stretchColumns="2"
android:layout_height="match_parent"
>
<TableRow
android:background="@drawable/border"
>
<TextView
android:id="@+id/tnameresult"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:text="@string/dem"
android:background="@drawable/border"
android:padding="3dip" />
<TextView
android:id="@+id/tcompresult"
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight="1"
android:text="@string/dem"
android:background="@drawable/border"
android:padding="3dip" />
</TableRow>
</TableLayout>
</LinearLayout>
</HorizontalScrollView>
这是我的表格布局显示,列的宽度和高度不会自动伸展。有人可以教我如何做吗?谢谢:)
不要使用HorizontalScrollView,您需要水平布局管理器。在您的java类中尝试以下代码:LinearLayoutManager layoutManager = new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL,false);RecyclerView myList =(RecyclerView)findViewById(R.id.my_recycler_view); myList.setLayoutManager(layoutManager);