gpt4 book ai didi

android - 在 onCreate() 之外的运行时动态添加 xml 定义的布局

转载 作者:行者123 更新时间:2023-11-30 04:23:29 24 4
gpt4 key购买 nike

我有一个自定义 ViewGroup(蓝色背景),当在应用程序生命周期的某个时候被触及时,我想向该自定义 View 添加任意 xml 定义的 View 布局(白框位于前景)。

除了 Activity 的 onCreate 之外,还有什么办法可以做到这一点吗?

白盒.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#FFFFFF"
android:orientation="vertical" >
</LinearLayout>

TestDynamicLayoutActivity.java

package net.lapasa.testdynamiclayout;

import android.app.Activity;
import android.os.Bundle;

public class TestDynamicLayoutActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(new MyCustomViewGroup(this));
}
}

MyCustomViewGroup.java

package net.lapasa.testdynamiclayout;

import java.lang.annotation.Target;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

public class MyCustomViewGroup extends ViewGroup
{

private Context context;

public MyCustomViewGroup(Context context)
{
super(context);
this.context = context;
}

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
this.setBackgroundColor(Color.BLUE);

}

@Override
public boolean onTouchEvent (MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
View whiteBoxView= LayoutInflater.from(context).inflate(R.layout.whitebox, this);
ViewGroup targetParent = (ViewGroup) whiteBoxView.getParent();
targetParent.removeView(whiteBoxView);
this.addView(whiteBoxView);
}
return true;
}
}

ACTION_DOWN 中的代码块除了显示黑屏外什么都不做 =( 发生了什么?

最佳答案

我说得对吗 - 您只是想要一些包含白色 View 的蓝色 View ,该白色 View 会在单击蓝色 View 时显示?

那么最好不要实现自己的ViewGroup,而是使用现有的FrameLayout 之一。将布局的背景设置为蓝色。添加一个子 View,其背景设置为白色,可见性设置为 View.INVISIBLEView.GONE

将点击处理程序添加到 ViewGroup,将白色 View 的可见性设置为 View.VISIBLE

你可以这样做:

主.xml:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:background="#00f"
android:onClick="onBlueClick"

android:layout_width="fill_parent"
android:layout_height="fill_parent">
<View android:id="@+id/whiteBox"
android:background="#fff"
android:visibility="gone"

android:layout_gravity="center"
android:layout_width="20dp"
android:layout_height="20dp"/>
</FrameLayout>

MyActivity.java:

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class MyActivity extends Activity {

private View whiteBox;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

whiteBox = findViewById(R.id.whiteBox);
}

@SuppressWarnings({"UnusedDeclaration"}) // wired by layout xml
public void onBlueClick(final View view) {
if (whiteBox != null) {
whiteBox.setVisibility(whiteBox.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
}
}
}

关于android - 在 onCreate() 之外的运行时动态添加 xml 定义的布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8915199/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com