Java 自定义视图滚动问题
我一直在设计一个需要在自定义视图上使用滚动条的应用程序。我遇到的问题是,当用户添加新项目时,自定义视图的高度将不断变化。我怀疑这不是一个非常不寻常的问题,但让事情复杂化的是,我实际上并没有向自定义视图添加新对象,我只是在画布上绘制位图图像。这对我来说很麻烦的原因是当Android放大布局时,它会将大小设置为我告诉它的任何值(0dip),当我添加位图时,它只会更改大小以填充滚动视图;因此,每当自定义视图绘制超出当前高度限制的图像时,应用程序不会滚动,而是简单地切断图片以保持当前高度Java 自定义视图滚动问题,java,android,android-custom-view,Java,Android,Android Custom View,我一直在设计一个需要在自定义视图上使用滚动条的应用程序。我遇到的问题是,当用户添加新项目时,自定义视图的高度将不断变化。我怀疑这不是一个非常不寻常的问题,但让事情复杂化的是,我实际上并没有向自定义视图添加新对象,我只是在画布上绘制位图图像。这对我来说很麻烦的原因是当Android放大布局时,它会将大小设置为我告诉它的任何值(0dip),当我添加位图时,它只会更改大小以填充滚动视图;因此,每当自定义视图绘制超出当前高度限制的图像时,应用程序不会滚动,而是简单地切断图片以保持当前高度 <com
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
(这是一款纸牌游戏应用程序,所以我想以一种易于查看和编辑的方式扇出您当前的纸牌。)
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
以下是相关代码:
public class DeckEdit extends Activity implements FilterQueryProvider {
static final int PROGRESS_DIALOG = 0;
private Deck deck;
private CardListAdapter lstAdpt;
private ArrayList<CardImage> cards;
private ProgressDialog progressDialog;
private ProgressThread progressThread;
private String[] cardList;
private DeckEditCardView deckGrid;
private ScrollView sc;
protected Cursor cursor;
private EditText filterText = null;
ArrayAdapter<String> adapter = null;
public void onCreate(Bundle savedInstanceBundle){
super.onCreate(savedInstanceBundle);
deck = new Deck();
//This is what stores the Card Images that I draw to the canvas with
cards = new ArrayList<CardImage>();
setContentView(R.layout.deck_edit_layout);
sc = (ScrollView) findViewById(R.id.deck_scroller);
deckGrid = (DeckEditCardView) findViewById(R.id.deck_grid);
filterText = (EditText) findViewById(R.id.card_search_box);
filterText.addTextChangedListener(filterTextWatcher);
pickDeck();
}
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
DeckEditCardView类包含以下内容
public class DeckEditCardView extends View implements OnTouchListener {
private ArrayList<CardImage> cards;
private int X_COLS;
@SuppressWarnings("unused")
private int Y_ROWS;
private final int COL_WIDTH;
private final int ROW_HEIGHT;
private Context context;
private final int X_PADDING = 2;
private final int Y_PADDING = 2;
private final int X_CARD_OVERLAY = 7;
private final int Y_CARD_OVERLAY = 5;
public DeckEditCardView(Context context) {
super(context);
cards = new ArrayList<CardImage>();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 1: "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 1: "+ROW_HEIGHT);
}
public DeckEditCardView(Context context, AttributeSet attrs) {
super(context, attrs);
cards = new ArrayList<CardImage>();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 2 : "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 2: "+ROW_HEIGHT);
}
/*
public DeckEditCardView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
cards = new ArrayList<CardImage>();
int width = getWidth();
int height = getHeight();
int cardWidth = getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardHeight = getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
COL_WIDTH = (X_PADDING+(X_CARD_OVERLAY*3)+cardWidth);
Log.d("DECV","COLWIDTH 3: "+COL_WIDTH);
ROW_HEIGHT = (Y_PADDING+(Y_CARD_OVERLAY*3)+cardHeight);
Log.d("DECV","ROWE_HEIGHT 3: "+ROW_HEIGHT);
X_COLS = (width/COL_WIDTH);
Y_ROWS = (height/ROW_HEIGHT);
}
*/
@Override
public void onFinishInflate(){
super.onFinishInflate();
//this.
}
public void onSizeChanged(int w, int h, int oldw, int oldh){
int width = getWidth();
int height = getHeight();
X_COLS = (width/COL_WIDTH);
Y_ROWS = (height/ROW_HEIGHT);
}
public Point getNextCardPoint(Point curP, boolean sameCard){
Point p=null;
if (sameCard)
p = new Point(curP.x+X_CARD_OVERLAY,curP.y+Y_CARD_OVERLAY);
else { //need to be on the same column
if (point2Col(curP)+1<X_COLS)
p = new Point(COL_WIDTH*(point2Col(curP)+1)+X_PADDING,
ROW_HEIGHT*(point2Row(curP))+Y_PADDING);
else {//add to new column
if (point2Row(curP)+1 > Y_ROWS)
Y_ROWS++;
p = new Point(X_PADDING,
ROW_HEIGHT*(point2Row(curP)+1)+Y_PADDING);
}
}
return p;
}
public void setCards(ArrayList<CardImage> cards){
this.cards = cards;
}
private int point2Col(Point p){
return p.x / COL_WIDTH;
}
private int point2Row(Point p){
return p.y / ROW_HEIGHT;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//ScrollView s = new ScrollView(context);
//s.addView(canvas);
int length = cards.size();
CardImage c;
for (int i = 0; i < length; i++) {
//Log.d("onDraw", "output a card");
c = cards.get(i);
canvas.drawBitmap(c.getBitmap(), c.getX(), c.getY(), null);
}
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
公共类DecketCardView扩展了视图实现到TouchListener上{
私人ArrayList卡;
私人int X_COLS;
@抑制警告(“未使用”)
私密的国际行;
专用最终内柱宽度;
私人最终整数行高;
私人语境;
私有最终整数X_PADDING=2;
私有最终整数Y_PADDING=2;
私人最终int X_卡覆盖=7;
私人最终积分卡叠加=5;
公共DeckEditCardView(上下文){
超级(上下文);
cards=新的ArrayList();
int cardWidth=getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardheath=getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
列宽度=(X_填充+(X_卡片重叠*3)+卡片宽度);
对数d(“DECV”,“COLWIDTH 1:”+COL_WIDTH);
行高=(Y_填充+(Y_卡片重叠*3)+卡片高度);
对数d(“DECV”,“行高1:+行高);
}
公共DeckEditCardView(上下文、属性集属性){
超级(上下文,attrs);
cards=新的ArrayList();
int cardWidth=getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardheath=getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
列宽度=(X_填充+(X_卡片重叠*3)+卡片宽度);
对数d(“DECV”,“COLWIDTH 2:+COL_WIDTH”);
行高=(Y_填充+(Y_卡片重叠*3)+卡片高度);
对数d(“DECV”,“行高2:+行高);
}
/*
公共DeckEditCardView(上下文上下文、属性集属性、int-defStyle){
超级(上下文、属性、定义样式);
cards=新的ArrayList();
int width=getWidth();
int height=getHeight();
int cardWidth=getResources().getDrawable(R.drawable.back).getIntrinsicWidth();
int cardheath=getResources().getDrawable(R.drawable.back).getIntrinsicHeight();
列宽度=(X_填充+(X_卡片重叠*3)+卡片宽度);
对数d(“DECV”,“COLWIDTH 3:+COL_WIDTH”);
行高=(Y_填充+(Y_卡片重叠*3)+卡片高度);
对数d(“DECV”,“行高3:+行高);
X_COLS=(宽度/列宽度);
Y_行=(高度/行高);
}
*/
@凌驾
公共空间膨胀(){
超级充气();
//这个。
}
已更改尺寸上的公共空隙(整数w、整数h、整数oldw、整数oldh){
int width=getWidth();
int height=getHeight();
X_COLS=(宽度/列宽度);
Y_行=(高度/行高);
}
公共点getNextCardPoint(点curP,布尔sameCard){
点p=零;
if(sameCard)
p=新点(电流x+x\U卡叠加,电流y+y\U卡叠加);
否则{//需要在同一列上
if(点2COL(curP)+1 Y_行)
Y_行++;
p=新点(X_),
行高*(点2Row(curP)+1)+Y_填充);
}
}
返回p;
}
公共作废设置卡(ArrayList卡){
这个。卡片=卡片;
}
专用int点2COL(点p){
返回p.x/柱宽;
}
专用int point2Row(点p){
返回p.y/行高;
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//ScrollView s=新的ScrollView(上下文);
//s、 addView(画布);
int length=cards.size();
心脏图像c;
for(int i=0;i
因此,总而言之,我不知道如何允许用户滚动并查看屏幕上不适合的卡片(几乎可以保证,一个完全构建的卡片组将使用滚动条查看所有卡片。使用此代码最多可以放入我需要的所有卡片图像,但屏幕上的图片我不知道该怎么做:/
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
最后是我的Layout.xml文件
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>
您的布局无法正常工作,因为
DecketEditCardView
无法自行测量。您必须覆盖视图.onMeasure()DecketEditCardView
类中的方法,并根据视图的内容设置测量的大小。在布局xml文件中,必须指定wrap\u content
作为DecketEditCardView
的高度,因为DecketEditCardView
本身无法测量,因此布局无法正常工作。必须覆盖使用DeckEditCardView
类中的View.onMeasure()
方法,并根据视图的内容设置测量尺寸。在布局xml文件中,必须将wrap\u content
指定为DeckEditCardView
的高度
<com.xeroxchange.ydd.DeckEditCardView android:layout_weight="2"
android:id="@+id/deck_grid"
android:layout_width="fill_parent"
android:layout_height="0dip"></com.xeroxchange.ydd.DeckEditCardView>
</ScrollView>
<LinearLayout android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<!-- Pretty hint text, and maxLines -->
<EditText android:id="@+id/card_search_box"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="type to filter"
android:inputType="text"
android:maxLines="1"/>
<!-- Set height to 0, and let the weight param expand it -->
<!-- Note the use of the default ID! This lets us use a
ListActivity still! -->
<ListView android:id="@+id/card_list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="2"
/>
</LinearLayout>
</LinearLayout>