Android 将水平线布局内容拉伸到全宽
我有一个问题需要将水平线性布局中的内容扩展到整个宽度。这似乎是一个微不足道的问题,但我在安卓方面并不是很有经验。我尝试了几种解决办法,但都没有奏效 我想创建一个具有固定标题的表,将其用作片段。它的主要结构如下所示:Android 将水平线布局内容拉伸到全宽,android,android-layout,Android,Android Layout,我有一个问题需要将水平线性布局中的内容扩展到整个宽度。这似乎是一个微不足道的问题,但我在安卓方面并不是很有经验。我尝试了几种解决办法,但都没有奏效 我想创建一个具有固定标题的表,将其用作片段。它的主要结构如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height=
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<android.widget.HorizontalScrollView
android:id="@+id/tableContentHorizontalScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@android:color/holo_blue_bright">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<LinearLayout
android:id="@+id/tableHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
android:orientation="horizontal"></LinearLayout>
<ScrollView
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dip"
android:fillViewport="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="horizontal|vertical">
<TableLayout
android:id="@+id/tableContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableLayout>
</ScrollView>
</LinearLayout>
</android.widget.HorizontalScrollView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:minWidth="150dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="vertical">
<TextView
android:id="@+id/headerCellTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12345679" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
列的数量可能会有所不同,如果列没有填满屏幕的整个宽度(如本例所示),那么这些列应该平均拉伸。您正在设置单元格android:maxWidth=“150dp”
,使其尽可能宽。尝试将宽度设置为0dp,权重为1,以使其填充均匀
android:layout_width="0dp"
android:layout_weight="1"
编辑:
我不知道这是否是最好的方法,但你总是可以做数学
使用getResources().getDisplayMetrics().widthPixels获取屏幕宽度,然后除以列数。获取结果并设置单元格宽度。你是对的,maxWidth没有意义。我只是太专注于表格标题部分,以至于忘了删除它。我也玩弄了重量,但我无法让它发挥作用。所以我已经试过你的建议了。我不喜欢所有的嵌套布局,但是类似的东西应该可以让你得到一个相等的单元格分布。奇怪的是,我第一次尝试的时候它不起作用。哦,我确实忽略了一些事情。现在它开始工作了!谢谢。我想,我最终还是要走这条路。我想这可能是最好的办法。我不确定我是否尝试过你发布的代码片段,但在过去,我没有最好的经验来获取设备的确切屏幕大小。也许新的Android版本改变了这一点,但不幸的是,我仍然使用5.1。但我会查出来的,谢谢。
android:layout_width="0dp"
android:layout_weight="1"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<android.widget.HorizontalScrollView
android:id="@+id/tableContentHorizontalScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="@android:color/holo_blue_bright">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_light"
android:orientation="vertical">
<LinearLayout
android:id="@+id/tableHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light"
android:orientation="horizontal">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/headerCellTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12" />
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/headerCellTextView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12" />
<Spinner
android:id="@+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/headerCellTextView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="12" />
<Spinner
android:id="@+id/spinner3"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<ScrollView
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dip"
android:fillViewport="true"
android:scrollbarStyle="outsideInset"
android:scrollbars="horizontal|vertical">
<TableLayout
android:id="@+id/tableContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/contentCellTextView"
android:layout_width="match_parent"
android:maxWidth="150dp"
android:layout_height="20dp"
android:text="0101010" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/contentCellTextView2"
android:layout_width="match_parent"
android:maxWidth="150dp"
android:layout_height="20dp"
android:text="12" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="@android:color/holo_blue_bright"
android:orientation="vertical"
android:layout_weight="1">
<TextView
android:id="@+id/contentCellTextView3"
android:layout_width="match_parent"
android:maxWidth="150dp"
android:layout_height="20dp"
android:text="10" />
</LinearLayout>
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
</android.widget.HorizontalScrollView>
</LinearLayout>