Sai*_*hna 16 android android-scrollview android-5.0-lollipop android-6.0-marshmallow android-percentrelativelayout
我创建了一个使用ScrollViewa PercentRelativeLayout作为其子项的布局.它不适用于Lollipop和旧设备,但在Marshmallow设备上工作正常.请检查以下代码:
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<android.support.percent.PercentRelativeLayout
android:id="@+id/scrollContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_heightPercent="100%"
app:layout_widthPercent="50%">
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_red_dark"
android:text="kkjknadko"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:text="Abcaad"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview2"
android:background="@android:color/holo_red_dark"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview3"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview4"
android:background="@android:color/holo_red_dark"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview5"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview6"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
<TextView
android:id="@+id/textview8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textview7"
android:text="Abcd"
android:textColor="@android:color/black"
app:layout_heightPercent="10%"
app:layout_marginTopPercent="10%"
app:layout_widthPercent="50%"/>
</android.support.percent.PercentRelativeLayout>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)
而且我android:fillViewport="true"也没有在Lollipop和较旧的Android版本中显示任何内容.
不幸的是,百分比布局ScrollView在M之前不起作用.原因在于它们取决于在测量步骤中传递的尺寸提示.在M之前,大多数布局在发送未指定的测量规范时会提供大小提示0.
您可以尝试创建自己的子类来解决这个问题
ScrollView和压倒一切的measureChild和measureChildWithMargins(幸好都被保护),提供大小提示.
来源 - plus.google.com.
有人可以帮助我创建自定义ScrollView以使其工作吗?
创建一个定制scrollview和设置Measured Height,并Width为您希望将其例
CustomScrollView
public class CustomScrollView extends ScrollView {
public CustomScrollView(Context context) {
super(context);
}
public CustomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int size = 0;
int width = getMeasuredWidth();
int height = getMeasuredHeight();
if (width > height) {
size = height;
} else {
size = width;
}
setMeasuredDimension(size, size);
}
}
Run Code Online (Sandbox Code Playgroud)
在xml中使用customscrollview.
<yourscrollviewclasspath.CustomScrollView // here you have scrollview path like com.yourpackage.folder_where_your_scrollview_lies
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
</yourscrollviewclasspath.CustomScrollView >
Run Code Online (Sandbox Code Playgroud)
据我了解,问题是:我希望这些 TextView 的宽度为父视图的 50%。
对我有用的方法是:
<Space
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="@+id/space" />
Run Code Online (Sandbox Code Playgroud)
然后将视图与其对齐。相对布局或文本视图。您将失去对相对布局百分比的控制(如果您曾经拥有的话)。我倾向于这样的事情:
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
Run Code Online (Sandbox Code Playgroud)
这有帮助吗?
| 归档时间: |
|
| 查看次数: |
1810 次 |
| 最近记录: |