Android 展开并指定要查看的布局后,将看不到布局中的元素
我正在为3D翻页编写一个自定义视图,该视图从视图扩展而来。在这个自定义视图中,我为页面的前景和背景声明了两个视图。我已经为页面的每个前景和背景声明了不同的布局。每个布局都有一个Relativelayout和其中的一些元素 在自定义视图中,我膨胀布局并将其分配给前景视图和背景视图 可以看到RelativeLayout,但布局中的元素不显示 有人能告诉我如何做到这一点吗。我卡住了 我的customview代码:Android 展开并指定要查看的布局后,将看不到布局中的元素,android,android-layout,Android,Android Layout,我正在为3D翻页编写一个自定义视图,该视图从视图扩展而来。在这个自定义视图中,我为页面的前景和背景声明了两个视图。我已经为页面的每个前景和背景声明了不同的布局。每个布局都有一个Relativelayout和其中的一些元素 在自定义视图中,我膨胀布局并将其分配给前景视图和背景视图 可以看到RelativeLayout,但布局中的元素不显示 有人能告诉我如何做到这一点吗。我卡住了 我的customview代码: public class PageCurlView extends View {
public class PageCurlView extends View {
/** Our Log tag */
private final static String TAG = "PageCurlView";
private Context myAppContext;
/** The context which owns us */
private WeakReference<Context> mContext;
/** LAGACY The current foreground */
//private Bitmap mForeground;
public View mForeground;
/** LAGACY The current background */
//private Bitmap mBackground;
public View mBackground;
/** LAGACY Current selected page */
private int mIndex = 0;
public Integer[] mViewIds = {
R.layout.view_1,
R.layout.view_2,
R.layout.view_3,
R.layout.view_4,
R.layout.view_5,
R.layout.view_6,
R.layout.view_7,
R.layout.view_8,
R.layout.view_9,
R.layout.view_10
};
private int mTotalViews = mViewIds.length;
//Variables for inline sliders
public ViewPager mInlinePager;
public AwesomePagerAdapter mInlineAdapter;
/**
* Base
* @param context
*/
public PageCurlView(Context context) {
super(context);
init(context);
ResetClipEdge();
}
/**
* Construct the object from an XML file. Valid Attributes:
*
* @see android.view.View#View(android.content.Context, android.util.AttributeSet)
*/
public PageCurlView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
// Get the data from the XML AttributeSet
{
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PageCurlView);
// Get data
bEnableDebugMode = a.getBoolean(R.styleable.PageCurlView_enableDebugMode, bEnableDebugMode);
mCurlSpeed = a.getInt(R.styleable.PageCurlView_curlSpeed, mCurlSpeed);
mUpdateRate = a.getInt(R.styleable.PageCurlView_updateRate, mUpdateRate);
mInitialEdgeOffset = a.getInt(R.styleable.PageCurlView_initialEdgeOffset, mInitialEdgeOffset);
mCurlMode = a.getInt(R.styleable.PageCurlView_curlMode, mCurlMode);
// recycle object (so it can be used by others)
a.recycle();
}
ResetClipEdge();
}
/**
* Initialize the view
*/
private final void init(Context context) {
myAppContext = context;
// Cache the context
mContext = new WeakReference<Context>(context);
// Base padding
setPadding(3, 3, 3, 3);
// The focus flags are needed
setFocusable(true);
setFocusableInTouchMode(true);
mMovement = new Vector2D(0,0);
mFinger = new Vector2D(0,0);
mOldMovement = new Vector2D(0,0);
// Set the default props, those come from an XML :D
// Create some sample images
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[0], null);
View view2 = inflater.inflate(mViewIds[1], null);
//Fix after coming back from vacation
//For inline sliders
mForeground = view1;
mBackground = view2;
}
/**
* Reset points to it's initial clip edge state
*/
/**
* Render the text
*
* @see android.view.View#onDraw(android.graphics.Canvas)
*/
//@Override
//protected void onDraw(Canvas canvas) {
// super.onDraw(canvas);
// canvas.drawText(mText, getPaddingLeft(), getPaddingTop() - mAscent, mTextPaint);
//}
//---------------------------------------------------------------
// Curling. This handles touch events, the actual curling
// implementations and so on.
//---------------------------------------------------------------
/**
* Swap between the fore and back-ground.
*/
@Deprecated
private void SwapViews() {
/*Bitmap temp = mForeground;
mForeground = mBackground;
mBackground = temp;*/
View temp = mForeground;
mForeground = mBackground;
mBackground = temp;
}
/**
* Swap to next view
*/
private void nextView() { //Sushil need to uncomment
int foreIndex = mIndex + 1;
if(foreIndex >= /*mPages.size()*/mTotalViews) {
//foreIndex = 0;
foreIndex = mTotalViews-1;
}
int backIndex = foreIndex + 1;
if(backIndex >= /*mPages.size()*/mTotalViews) {
//backIndex = 0;
backIndex = mTotalViews-1;
}
mIndex = foreIndex;
setViews(foreIndex, backIndex);
}
/**
* Swap to previous view
*/
private void previousView() { //Sushil need to uncomment
Log.i("Sushil", "....previousView()....");
int backIndex = mIndex;
int foreIndex = backIndex - 1;
if(foreIndex < 0) {
foreIndex = /*mPages.size()*/0;
}
mIndex = foreIndex;
setViews(foreIndex, backIndex);
}
/**
* Set current fore and background
* @param foreground - Foreground view index
* @param background - Background view index
*/
private void setViews(int foreground, int background) {
LayoutInflater inflater = (LayoutInflater) myAppContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1 = inflater.inflate(mViewIds[foreground], null);
View view2 = inflater.inflate(mViewIds[background], null);
mForeground = view1;//(WebView)mPages.get(foreground);
mBackground = view2;//(WebView)mPages.get(background);
}
//---------------------------------------------------------------
// Drawing methods
//---------------------------------------------------------------
@Override
protected void onDraw(Canvas canvas) {
// Always refresh offsets
mCurrentLeft = getLeft();
mCurrentTop = getTop();
// Translate the whole canvas
//canvas.translate(mCurrentLeft, mCurrentTop);
// We need to initialize all size data when we first draw the view
if ( !bViewDrawn ) {
bViewDrawn = true;
onFirstDrawEvent(canvas);
}
canvas.drawColor(Color.WHITE);
// Curl pages
//DoPageCurl();
// TODO: This just scales the views to the current
// width and height. We should add some logic for:
// 1) Maintain aspect ratio
// 2) Uniform scale
// 3) ...
Rect rect = new Rect();
rect.left = 0;
rect.top = 0;
rect.bottom = getHeight();
rect.right = getWidth();
// First Page render
Paint paint = new Paint();
// Draw our elements
drawForeground(canvas, rect, paint);
drawBackground(canvas, rect, paint);
drawCurlEdge(canvas);
// Draw any debug info once we are done
if ( bEnableDebugMode )
drawDebug(canvas);
// Check if we can re-enable input
if ( bEnableInputAfterDraw )
{
bBlockTouchInput = false;
bEnableInputAfterDraw = false;
}
// Restore canvas
//canvas.restore();
super.onDraw(canvas);
}
/**
* Called on the first draw event of the view
* @param canvas
*/
protected void onFirstDrawEvent(Canvas canvas) {
mFlipRadius = getWidth();
ResetClipEdge();
DoPageCurl();
}
/**
* Draw the foreground
* @param canvas
* @param rect
* @param paint
*/
private void drawForeground( Canvas canvas, Rect rect, Paint paint ) {
//canvas.drawBitmap(mForeground, null, rect, paint);
//mForeground.loadUrl("file:///android_asset/WebContent/Section01.html");
mForeground.layout(rect.left, rect.top, rect.right, rect.bottom);
mForeground.draw(canvas);
// Draw the page number (first page is 1 in real life :D
// there is no page number 0 hehe)
//drawPageNum(canvas, mIndex);
}
/**
* Create a Path used as a mask to draw the background page
* @return
*/
private Path createBackgroundPath() {
Path path = new Path();
path.moveTo(mA.x, mA.y);
path.lineTo(mB.x, mB.y);
path.lineTo(mC.x, mC.y);
path.lineTo(mD.x, mD.y);
path.lineTo(mA.x, mA.y);
return path;
}
/**
* Draw the background image.
* @param canvas
* @param rect
* @param paint
*/
private void drawBackground( Canvas canvas, Rect rect, Paint paint ) {
Path mask = createBackgroundPath();
// Save current canvas so we do not mess it up
canvas.save();
canvas.clipPath(mask);
//canvas.drawBitmap(mBackground, null, rect, paint);
//mBackground.loadUrl("file:///android_asset/WebContent/Section01.html");
mBackground.layout(rect.left, rect.top, rect.right, rect.bottom);
mBackground.draw(canvas);
// Draw the page number (first page is 1 in real life :D
// there is no page number 0 hehe)
drawPageNum(canvas, mIndex);
canvas.restore();
}
}
public类PageCurlView扩展了视图{
/**我们的日志标签*/
私有最终静态字符串TAG=“PageCurlView”;
私有上下文myAppContext;
/**拥有我们的环境*/
私有WeakReference mContext;
/**当前前景*/
//私人停车场;
公众视野;
/**当前的背景*/
//私人背景;
公众视野;
/**当前所选页面的长度*/
私有int mIndex=0;
公共整数[]mViewIds={
R.layout.view_1,
R.layout.view_2,
R.layout.view_3,
R.layout.view_4,
R.layout.view_5,
R.layout.view_6,
R.layout.view_7,
R.layout.view_8,
R.layout.view_9,
R.layout.view_10
};
private int mTotalViews=mViewIds.length;
//内联滑块的变量
公共视图寻呼机mInlinePager;
公共AwesomePagerAdapter mInlineAdapter;
/**
*基地
*@param上下文
*/
公共页面卷曲视图(上下文){
超级(上下文);
init(上下文);
ResetClipEdge();
}
/**
*从XML文件构造对象。有效属性:
*
*@see android.view.view#view(android.content.Context,android.util.AttributeSet)
*/
公共页面卷曲视图(上下文、属性集属性){
超级(上下文,attrs);
init(上下文);
//从XML属性集获取数据
{
TypedArray a=context.actainStyledAttributes(attrs,R.styleable.PageCurlView);
//获取数据
bEnableDebugMode=a.getBoolean(R.styleable.PageCurlView_enableddebugmode,bEnableDebugMode);
mCurlSpeed=a.getInt(R.styleable.PageCurlView\u curlSpeed,mCurlSpeed);
mUpdateRate=a.getInt(R.styleable.PageCurlView_updateRate,mUpdateRate);
minitialgeoffset=a.getInt(R.styleable.PageCurlView\u initialEdgeOffset,minitialgeoffset);
mCurlMode=a.getInt(R.styleable.PageCurlView\u curlMode,mCurlMode);
//回收对象(以便其他人可以使用)
a、 回收();
}
ResetClipEdge();
}
/**
*初始化视图
*/
私有最终void init(上下文){
myAppContext=context;
//缓存上下文
mContext=新的WeakReference(上下文);
//垫底
设置填充(3,3,3,3);
//需要焦点标志
设置聚焦(真);
setFocusableInTouchMode(真);
mMovement=新矢量2D(0,0);
mFinger=新向量2d(0,0);
mOldMovement=新矢量2D(0,0);
//设置默认的道具,这些道具来自XML:D
//创建一些示例图像
LayoutInflater充气器=(LayoutInflater)myAppContext.getSystemService(Context.LAYOUT\u充气器\u服务);
视图1=充气机。充气(mViewIds[0],空);
视图2=充气机充气(mViewIds[1],空);
//度假回来后修理
//用于内联滑块
mForeground=view1;
mBackground=view2;
}
/**
*将点重置为其初始剪辑边状态
*/
/**
*渲染文本
*
*@see android.view.view#onDraw(android.graphics.Canvas)
*/
//@凌驾
//受保护的void onDraw(画布){
//super.onDraw(帆布);
//drawText(mText,getPaddingLeft(),getPaddingTop()-mAscent,mTextPaint);
//}
//---------------------------------------------------------------
//卷发。这处理触摸事件,实际的卷发
//实现等等。
//---------------------------------------------------------------
/**
*在前后地面之间交换。
*/
@不赞成
私有void swapview(){
/*位图温度=mForeground;
mForeground=mBackground;
mBackground=温度*/
视图温度=mForeground;
mForeground=mBackground;
mBackground=温度;
}
/**
*切换到下一个视图
*/
private void nextView(){//Sushil需要取消注释
int foreIndex=mIndex+1;
如果(foreIndex>=/*mPages.size()*/mTotalViews){
//foreIndex=0;
foreIndex=mtotalView-1;
}
int backIndex=foreIndex+1;
如果(反向索引>=/*mPages.size()*/mTotalViews){
//反向指数=0;
backIndex=mTotalViews-1;
}
mIndex=外汇指数;
集合视图(前向索引、后向索引);
}
/**
*切换到上一个视图
*/
private void previousView(){//Sushil需要取消注释
Log.i(“Sushil”,“previousView()”);
int backIndex=mIndex;
int foreIndex=backIndex-1;
如果(前置索引<0){
foreIndex=/*mPages.size()*/0;
}
mIndex=外汇指数;
集合视图(前向索引、后向索引);
}
/**
*设定当前的前导和背景
*@param前台-前台视图索引
*@param background-背景视图索引
*/
私有void集合视图(int前景、int背景){
LayoutFlater充气器=(LayoutFlater)myAppContext.getSystemService(Context.LAYOUT\u充气器\u服务)
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content">
<TextView
android:id="@+id/sampletextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/red"
android:text="VIEW 1" />
</RelativeLayout>
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams([width], [heigth]);
//assign additional params like below, above, to right or to left or others relative layout params
inflatedView.setLayoutParams(lp);