[英]How do I add a LinearLayoutCompat view to an existing xml layout dynamically?
[英]How can I include a custom view in an existing XML layout on Android?
我已按照本教程( http://stuffthathappens.com/blog/2008/11/13/android-animation-101/ )进行操作,并在画布上绘制了漂亮的旋转文字。 当我的主要活动使用时,一切正常
setContentView(new SplashScreenAnimation(this));
这是SplashScreenAnimation:
public class SplashScreenAnimation extends View {
private static final String QUOTE = "Nobody uses Java anymore. It's this big heavyweight ball and chain.";
private Animation anim;
public SplashScreenAnimation(Context context) {
super(context);
}
private void createAnim(Canvas canvas) {
anim = new RotateAnimation(0, 360, canvas.getWidth() / 2, canvas
.getHeight() / 2);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(10000L);
anim.setInterpolator(new AccelerateDecelerateInterpolator());
startAnimation(anim);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// Creates the animation the first time
if (anim == null) {
createAnim(canvas);
}
Path circle = new Path();
int centerX = canvas.getWidth() / 2;
int centerY = canvas.getHeight() / 2;
int r = Math.min(centerX, centerY);
circle.addCircle(centerX, centerY, r, Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.BLUE);
paint.setTextSize(30);
paint.setAntiAlias(true);
canvas.drawTextOnPath(QUOTE, circle, 0, 30, paint);
}
}
但是我想组合这个动画文本并在其下面有两个按钮,所以我有一个RelativeLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@drawable/splash_screen">
<LinearLayout android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_alignParentBottom="true" android:id="@+id/bottomLinearLayout">
<Button android:layout_gravity="center_horizontal" android:id="@+id/playButton"
android:text="Button" android:layout_height="wrap_content"
android:layout_width="wrap_content"></Button>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content" android:id="@+id/optionsButton"
android:text="Button" android:layout_gravity="center_horizontal"
android:layout_marginBottom="30sp"></Button>
</LinearLayout>
<LinearLayout android:id="@+id/topLinearLayout"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_width="fill_parent">
</LinearLayout>
</RelativeLayout>
如何将此自定义视图添加到上面的LinearLayout中? (topLinearLayout-这是最后一个)
我尝试了很多不同的方法,但最终还是以强制关闭告终。
这种方法主要是我尝试过的方法。 我试过充气,等等。
LinearLayout item = (LinearLayout)findViewById(R.id.topLinearLayout);
SplashScreenAnimation child = new SplashScreenAnimation(this);
item.addView(child);
我添加了整个SplashScreenAnimation类代码:
((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))
有用!!
为什么其他手动添加方法不起作用? 这与动画需要通过onDraw
开始有关吗?
您应该能够像在布局中的任何其他视图一样包含它。
请尝试以下操作,然后将内容视图设置为整个XML布局:
<LinearLayout android:id="@+id/topLinearLayout"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:layout_width="fill_parent">
<com.YOURPACKAGENAME.SplashScreenAnimation
android:id="@+id/animation"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
</LinearLayout>
这很容易,您只需要包含扩展View类的类的完整程序包名称即可:
<LinearLayout
android:id="@+id/topLinearLayout"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_width="fill_parent">
<mypackage.SplashScreenAnimation
android:layout_height="wrap_content"
android:layout_width="fill_parent"/>
</LinearLayout>
尝试以下行添加视图:
((LinearLayout) findViewById(R.id.topLinearLayout)).addView(new SplashScreenAnimation(this))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.