【发布时间】:2012-10-12 00:43:22
【问题描述】:
我一直在我的应用程序中使用滚动视图,但问题是这是我第一次绘制比默认尺寸屏幕更长的东西。
XML 设计屏幕显示固定的 3.7 英寸(或更大)屏幕。如果我想添加更多对象怎么办?在 Eclipse 中设计时如何向下滚动?
【问题讨论】:
标签: android xml scrollview
我一直在我的应用程序中使用滚动视图,但问题是这是我第一次绘制比默认尺寸屏幕更长的东西。
XML 设计屏幕显示固定的 3.7 英寸(或更大)屏幕。如果我想添加更多对象怎么办?在 Eclipse 中设计时如何向下滚动?
【问题讨论】:
标签: android xml scrollview
这是我的解决方案:我将 ScrollView 子类化为采用仅在 isInEditMode() 时使用的 scrollY 参数。
public class MyScollView extends ScrollView {
private int scrollY = 0;
private void init(Context context, AttributeSet attrs) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MyScrollView);
scrollY = a.getInt(R.styleable.MyScrollView_scrollY, 0);
a.recycle();
}
public MyScollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
public MyScollView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public MyScollView(Context context) {
super(context);
}
@Override
public void computeScroll() {
if (isInEditMode()) {
scrollTo(0, scrollY);
}
}
}
这将进入您的 attrs.xml 文件
<declare-styleable name="MyScrollView">
<attr name="scrollY" format="integer"/>
</declare-styleable>
【讨论】:
将类似android:layout_marginTop="-300dp" 的属性添加到直接ScrollView 子级。
【讨论】:
我通常只使用负边距。它适用于滚动视图,就像在里面的项目上一样。
【讨论】:
将android:scrollY="300dp" 添加到您的布局中。 Tt 会将内容向上滚动 300dp。
在将 android:scrollY="300dp" 添加到布局之前
<RelativeLayout 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" tools:context="com.javamad.user_profile"
>
你看不到设计空间
将 android:scrollY="300dp" 添加到布局后
<RelativeLayout 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" tools:context="com.javamad.user_profile"
android:scrollY="300dp"
>
你看到了设计的空间。
【讨论】:
从 Android API 19 开始,此解决方案已更改。 这对我有用。
转到您的 Android 虚拟设备管理器(在 Eclipse 中,这是在 Window > Android Virtual Device Manager 中),然后转到“设备定义”选项卡并创建一个新设备。将其命名为“Long Scroller”等令人难忘的名称。
在编辑/创建设备屏幕中,将垂直尺寸设置为您认为滚动视图的长度(7000 像素对于我的使用来说已经足够了)。您也可以稍后对其进行调整。
重新启动 Eclipse,打开长滚动视图布局,您的长滚动条将出现在预览布局中。
【讨论】:
<RelativeLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".AdminControlPanel"
android:scrollY="200dp">
这个 android:scrollY 让你的设计屏幕在 Y 方向上更长...向下添加你的组件
【讨论】:
您可以将屏幕尺寸更改为自定义.. 尝试这个.. 单击当前屏幕按钮,然后选择“添加自定义”,然后向下滚动并选择自定义,然后单击右上角的“新建”。 然后为屏幕编写自定义尺寸并单击“确定”,然后再次单击“确定”.. 然后再次单击您当前的屏幕按钮并选择您的自定义尺寸.. 完成! 希望有所帮助!
确保选中此按钮。
【讨论】:
尝试将您的内部布局设置为固定高度。所以你的代码应该是这样的
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_height="900dp"
>
</ReleativeLayout>
</ScrollView>
这应该可以工作
【讨论】:
尝试在ScrollView 上临时设置android:scrollY。我还没有尝试过,但理论上它应该可以工作。只需记住在发布应用时删除该属性即可。
【讨论】:
100dp。
tools:scrollY 属性。