Android布局均匀间距
下面是我的编辑器和设备显示内容的比较: 问题是: 包含TextView的行应占据屏幕的50% 包含图标的行应占据屏幕剩余的50% 它在编辑器中正常工作,但正如您所看到的,布局在实际设备上的行为大不相同 我的问题在哪里?如何解决 用于图标的库是: 它直接从ImageView扩展 布局XML:Android布局均匀间距,android,android-layout,layout,textview,imageview,Android,Android Layout,Layout,Textview,Imageview,下面是我的编辑器和设备显示内容的比较: 问题是: 包含TextView的行应占据屏幕的50% 包含图标的行应占据屏幕剩余的50% 它在编辑器中正常工作,但正如您所看到的,布局在实际设备上的行为大不相同 我的问题在哪里?如何解决 用于图标的库是: 它直接从ImageView扩展 布局XML: <TableLayout android:id="@+id/menuLayout" android:layout_width="match_parent" android:la
<TableLayout
android:id="@+id/menuLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="*" >
<TableRow
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_weight="1"
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/teenSelection"
android:textColor="@color/WHITE"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="TEEN" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/adultSelection"
android:textColor="@color/WHITE"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="ADULT"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/seniorSelection"
android:textColor="@color/WHITE"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="SENIOR"/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<net.steamcrafted.materialiconlib.MaterialIconView
android:layout_weight="1"
android:id="@+id/confirmButton"
app:materialIcon="check"
app:materialIconColor="#fff"
android:scaleType="center"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</TableRow>
</TableLayout>
您已将两个TableRows的高度设置为包裹内容,但它们必须是匹配父项,如下所示:
<TableRow
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent">
否则,高度的权重将被忽略。您已将两个表格行的高度设置为包裹内容,但它们必须是匹配的父级,如下所示:
<TableRow
android:layout_weight="1"
android:layout_height="match_parent"
android:layout_width="match_parent">
否则,高度的权重将被忽略。将表格行添加到线性布局中,如下所示。 我工作过。我做了测试
<TableLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:stretchColumns="*">
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/teenSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:background="@color/black_slow"
android:textStyle="bold"
android:textSize="30sp"
android:text="TEEN" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/adultSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:background="@color/black_slowest"
android:textStyle="bold"
android:textSize="30sp"
android:text="ADULT"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/seniorSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="SENIOR"/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/confirmButton"
app:materialIcon="check"
app:materialIconColor="#fff"
android:scaleType="center"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</TableRow>
</LinearLayout>
</TableLayout>
像这样在LinearLayout中添加TableRow。 我工作过。我做了测试
<TableLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:stretchColumns="*">
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TableRow
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_height="match_parent"
android:layout_width="match_parent">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/teenSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:background="@color/black_slow"
android:textStyle="bold"
android:textSize="30sp"
android:text="TEEN" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/adultSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:background="@color/black_slowest"
android:textStyle="bold"
android:textSize="30sp"
android:text="ADULT"/>
<android.support.v7.widget.AppCompatTextView
android:id="@+id/seniorSelection"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:background="@color/colorPrimaryDark"
android:layout_weight="1"
android:textStyle="bold"
android:textSize="30sp"
android:text="SENIOR"/>
</LinearLayout>
</TableRow>
<TableRow
android:layout_weight="1"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/confirmButton"
app:materialIcon="check"
app:materialIconColor="#fff"
android:scaleType="center"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</TableRow>
</LinearLayout>
</TableLayout>
你应该试试看
在应用程序级别build.gradle中添加此
用这个改变你的布局
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
app:layout_heightPercent="50%"
app:layout_widthPercent="100%">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/teenSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TEEN"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/adultSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="ADULT"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/seniorSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="SENIOR"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/confirmButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/layoutTop"
android:scaleType="center"
app:layout_heightPercent="50%"
app:layout_widthPercent="100%"
app:materialIcon="check"
app:materialIconColor="#fff" />
</android.support.percent.PercentRelativeLayout>
你应该试试看
在应用程序级别build.gradle中添加此
用这个改变你的布局
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/layoutTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
app:layout_heightPercent="50%"
app:layout_widthPercent="100%">
<android.support.v7.widget.AppCompatTextView
android:id="@+id/teenSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="TEEN"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/adultSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="ADULT"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
<android.support.v7.widget.AppCompatTextView
android:id="@+id/seniorSelection"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="SENIOR"
android:textColor="@android:color/white"
android:textSize="30sp"
android:textStyle="bold" />
</LinearLayout>
<net.steamcrafted.materialiconlib.MaterialIconView
android:id="@+id/confirmButton"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="@+id/layoutTop"
android:scaleType="center"
app:layout_heightPercent="50%"
app:layout_widthPercent="100%"
app:materialIcon="check"
app:materialIconColor="#fff" />
</android.support.percent.PercentRelativeLayout>
您是否尝试在第一个TableRow中设置android:layout_weight=0.5?我认为问题在于您的TableRow。您已将“权重”设置为1,但“布局高度”是“包裹内容”。改用match_parent…正如Miguel Benitez指出的,您是否尝试将android_layout_weigth设置为0.5?问题已解决,谢谢大家的帮助:您是否尝试将android:layout_weigth设置为第一个表格行中的0.5?我想问题在于您的表格行。您已将“权重”设置为1,但“布局高度”是“包裹内容”。改用match_parent…正如米格尔·贝尼特斯所指出的,您是否尝试将android_layout_weigth设置为0.5?问题解决了,谢谢大家的帮助:谢谢,我已经用mehrdad方法解决了。不过,谢谢你的支持,我从来都不知道有这样的事情存在;谢谢,我已经用mehrdad的方法解决了。不过,谢谢你的支持,我从来都不知道有这样的事情存在;