今天用到了Scrolliew,从网上查了一下,有几点疑问,求前辈们给予指导,谢谢!
疑问一:
现象描述:xml中配置ScrollView,直接在其下面加一个系统的组件:例如,TextView或者Button等都是可以显示的。
但是换成自定义的View就不能显示(代码如下)。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<com.TestLayout.ResultDetailsView
android:layout_width="800px"
android:layout_height="400px">
</com.TestLayout.ResultDetailsView>
</ScrollView>
不知道什么原因,求指导,谢谢!
疑问二:
现象描述:xml中配置ScrollView,不在下面直接放自定义View了,而是先放个LinearLayout,然后在LinearLayout中再放自定义View,这样如果自定义View中属性:android:layout_width和android:layout_height设定的是固定的值,则该自定义View始可以显示的。如果用的是:android:layout_width="match_parent"和android:layout_height="match_parent"则无法显示:(代码如下:)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.TestLayout1.ResultDetailsView
android:layout_width="800px"
android:layout_height="400px">
</com.TestLayout1.ResultDetailsView>
</LinearLayout>
</ScrollView>
这样始可以显示的,但是如果将
<com.TestLayout1.ResultDetailsView
android:layout_width="800px"
android:layout_height="400px">
</com.TestLayout1.ResultDetailsView>
改成:
<com.TestLayout1.ResultDetailsView
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.TestLayout1.ResultDetailsView>
就显示不出来。
求指导,谢谢!谢谢!
疑问一:
现象描述:xml中配置ScrollView,直接在其下面加一个系统的组件:例如,TextView或者Button等都是可以显示的。
但是换成自定义的View就不能显示(代码如下)。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<com.TestLayout.ResultDetailsView
android:layout_width="800px"
android:layout_height="400px">
</com.TestLayout.ResultDetailsView>
</ScrollView>
不知道什么原因,求指导,谢谢!
疑问二:
现象描述:xml中配置ScrollView,不在下面直接放自定义View了,而是先放个LinearLayout,然后在LinearLayout中再放自定义View,这样如果自定义View中属性:android:layout_width和android:layout_height设定的是固定的值,则该自定义View始可以显示的。如果用的是:android:layout_width="match_parent"和android:layout_height="match_parent"则无法显示:(代码如下:)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<com.TestLayout1.ResultDetailsView
android:layout_width="800px"
android:layout_height="400px">
</com.TestLayout1.ResultDetailsView>
</LinearLayout>
</ScrollView>
这样始可以显示的,但是如果将
<com.TestLayout1.ResultDetailsView
android:layout_width="800px"
android:layout_height="400px">
</com.TestLayout1.ResultDetailsView>
改成:
<com.TestLayout1.ResultDetailsView
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.TestLayout1.ResultDetailsView>
就显示不出来。
求指导,谢谢!谢谢!
解决方案 »
- 初学,问个浏览器和底层之间通讯的问题
- 求大神解答framebuffer fb0文件读取权限问题
- android真机上调用本机webservice,地址该如何写?
- 急求,线程问题,求解释..
- 移动图片
- 怎样去条全局状态栏(statubar显示时间和电池状态)
- 关于PopupWindow的问题!!!!!!1
- 调用android自带的json类解析出错!!!
- 新手!求助在线自动更新之后,为什么总是提示更新,怎么修改versionCdoe的值
- AndroidStudio混淆打包出错:proguard.ParseException: Unknown option 'public' in line 49
- 求android2.2 android2.3 源代码!
- N个高手没有解决的难题!
还有疑问二好像也不是,因为自定义View中有好多TextView和Button的。
谢谢 啊!
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:scrollbars="vertical">
<com.fm.TestForCsdn.MyView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="ajaskjdk" />
</ScrollView>
自定义View的代码:我又写了一个很简单的(谅解com.TestLayout1.ResultDetailsView这个不敢贴,你懂的,哈哈),继承一个ViewGroup,里面addView()了一个button和一个TextView。
代码如下:
package com.TestLayout1;import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;public class MyView1 extends ViewGroup { private Button mButton = null;
private TextView mTextView = null;
private int mWidth;
private int mHeight;
public MyView1(Context context) {
this(context, null, 0);
}
public MyView1(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mButton = new Button(context);
mButton.setText("谢谢,平小哥!");
addView(mButton);
mTextView = new TextView(context);
mTextView.setText("!!!!!!!!!!!!!!!!!!!!!!!");
addView(mTextView);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight= getMeasuredHeight();
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int left, top, right, bottom;
left = 0;
top = 0;
right = mWidth / 2;
bottom = mHeight / 2;
mButton.layout(left, top, right, bottom);
top = bottom;
right = mWidth;
bottom = mHeight * 3 / 4;
mTextView.layout(left, top, right, bottom); }}
然后在xml中用,还是出现同样的问题!不知道怎么解决,谢谢啊!
PS:看到你回帖,我就开始写上面这个自定义view,结果一放到xml中就报这个错:The specified child already has a parent. You must call removeView() on the child's parent first. 我就开始找原因,感觉这么简单的一个View应该没有错误,找了好久!原来是:我addView()里面全部写的是同一个Button的实例。悲剧啊,,,,,,见笑了!哈哈!还有可能不是自定义View的问题。
将自定义View在xml中不套scrollview直接放在LinearLayout中,然后在Activity中加载是没有问题的。
但是加上后就不显示。
上面的意思是不是在自定义View的onMeasure()方法中加上setMeasuredDimension(200,200)?谢谢啊!~
自定义View:添加了setMeasuredDimension(800,400)
package com.TestLayout1;import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;public class MyView1 extends ViewGroup { private Button mButton = null;
private TextView mTextView = null;
private int mWidth;
private int mHeight;
public MyView1(Context context) {
this(context, null, 0);
}
public MyView1(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MyView1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mButton = new Button(context);
mButton.setText("谢谢,平小哥!");
addView(mButton);
mTextView = new TextView(context);
mTextView.setText("!!!!!!!!!!!!!!!!!!!!!!!");
addView(mTextView);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mWidth = getMeasuredWidth();
mHeight= getMeasuredHeight();
setMeasuredDimension(800, 400);
} @Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
int left, top, right, bottom;
left = 0;
top = 0;
right = mWidth / 2;
bottom = mHeight / 2;
mButton.layout(left, top, right, bottom);
top = bottom;
right = mWidth;
bottom = mHeight * 3 / 4;
mTextView.layout(left, top, right, bottom); }}XML代码如下:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical">
<com.TestLayout1.MyView1
android:layout_width="match_parent"
android:layout_height="wrap_content">
</com.TestLayout1.MyView1>
</ScrollView>最后在Activity中加载上面xml。没有显示,谢谢指教!
谢谢啊,不过我在onMeasure()中打了一下Log输出了一下
mWidth和mHeight,得到的值是:800和0,估计始这里的错误,不过不知到怎么改,谢谢啊!