如何将 PercenRelativeLayout 用于 listView 项目
How to use PercenRelativeLayout for listView item
我正在尝试将 PercenRelativeLayout 用于 ListView, 但它不起作用,
高度和宽度百分比被忽略,列表视图中没有显示任何内容。
它仅适用于 marshmallows.
这是列表项 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#d20404"
android:id="@+id/test_image_id"
android:layout_width="match_parent"
android:layout_height="300dp" />
<TextView
android:background="#000"
android:text="sfgfashdsfg"
android:layout_below="@+id/test_image_id"
app:layout_heightPercent="50%"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.percent.PercentRelativeLayout>
我在 github
上有一个示例项目
我已将此问题提交给 google。 答案是
The height of your ListView row item is insufficiently specified.
The first child says it wants to take 60% of the row height (note that
this is heightPercent and not aspectRatio), and the second child says
it wants to take 10% of the row height. But nothing tells ListView how
tall the entire row wants to be. So it ends up being with height 0.
Note that the semantics of height=match_parent do not work in ListView
rows, and if this works in any one particular Android platform version
(for however much you can say it works), it is purely incidental.
我遇到了与 android K 和 android L 相同的问题。
The percent layout won't work correctly before android M. The reason is that they depend on the size hint being delivered in the measuring step. Before android M most layouts would provide size hint 0.
将您的百分比相对布局转换为相对布局,并根据您的设备高度以编程方式设置视图的高度。
这不是一个非常优雅的解决方案,但它对我有用。
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:orientation="vertical">
<ImageView
android:id="@+id/test_image_id"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#d20404" />
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/test_image_id"
android:background="#000"
android:text="sfgfashdsfg" />
</RelativeLayout>
Java代码:
// to get the height of the screen
int height = getWindowManager().getDefaultDisplay().getHeight();
// to set height to each textview to 50% of screen
textView1.getLayoutParams().height = (int) (height * 0.50);
我正在尝试将 PercenRelativeLayout 用于 ListView, 但它不起作用, 高度和宽度百分比被忽略,列表视图中没有显示任何内容。 它仅适用于 marshmallows.
这是列表项 xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#d20404"
android:id="@+id/test_image_id"
android:layout_width="match_parent"
android:layout_height="300dp" />
<TextView
android:background="#000"
android:text="sfgfashdsfg"
android:layout_below="@+id/test_image_id"
app:layout_heightPercent="50%"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.percent.PercentRelativeLayout>
我在 github
上有一个示例项目我已将此问题提交给 google。 答案是
The height of your ListView row item is insufficiently specified.
The first child says it wants to take 60% of the row height (note that this is heightPercent and not aspectRatio), and the second child says it wants to take 10% of the row height. But nothing tells ListView how tall the entire row wants to be. So it ends up being with height 0.
Note that the semantics of height=match_parent do not work in ListView rows, and if this works in any one particular Android platform version (for however much you can say it works), it is purely incidental.
我遇到了与 android K 和 android L 相同的问题。
The percent layout won't work correctly before android M. The reason is that they depend on the size hint being delivered in the measuring step. Before android M most layouts would provide size hint 0.
将您的百分比相对布局转换为相对布局,并根据您的设备高度以编程方式设置视图的高度。
这不是一个非常优雅的解决方案,但它对我有用。
XML代码:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:orientation="vertical">
<ImageView
android:id="@+id/test_image_id"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#d20404" />
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/test_image_id"
android:background="#000"
android:text="sfgfashdsfg" />
</RelativeLayout>
Java代码:
// to get the height of the screen
int height = getWindowManager().getDefaultDisplay().getHeight();
// to set height to each textview to 50% of screen
textView1.getLayoutParams().height = (int) (height * 0.50);