android - 是否可以在 Android 中创建没有 xml 的 View ?

标签 android android-layout android-view android-viewgroup

我正在尝试创建一个没有 xml 的自定义 View

public class MyView extends ViewGroup {

    public MyView (Context context) {
        super(context);
        setBackgroundColor(0xffff0000);

        RelativeLayout base = new RelativeLayout(context);

        RelativeLayout.LayoutParams rP = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        base.setBackgroundColor(0xff00ff00);
        base.setLayoutParams(rP);

        RelativeLayout.LayoutParams iP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        ImageView icon = new ImageView(context);
        icon.setBackgroundResource(android.R.drawable.ic_menu_add);
        icon.setLayoutParams(iP);

        addView(icon);
        addView(base);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

    }
}

并在 actitvy 中使用它

MyView myview = new MyView(this);
WindowManager.LayoutParams baseParams = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.MATCH_PARENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);
addContentView(myview, baseParams);

但是 LayoutParams 不起作用,它什么也不显示

我必须使用这个onLayout来使其显示,并且基本布局不是MATCH_PARENT并且图标不是WRAP_CONTENT

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    final View child = getChildAt(0);
    final View child2 = getChildAt(1);

    child.layout(0, 0, 300, 300);
    child2.layout(0, 0, 300, 300);
}

我还尝试向布局添加图标,但应用程序会崩溃

base.addView(icon);

是否可以在不硬编码大小和位置的情况下创建此布局?

我检查了RelativeLayout.LayoutParams,但找不到任何方法将其centerInParent设置为true。

http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@android:drawable/ic_menu_add"
        android:layout_centerInParent="true"
        />
</RelativeLayout>

最佳答案

请尝试一下此代码。

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Create parent RelativeLayout
        RelativeLayout relParent = new RelativeLayout(this);
        RelativeLayout.LayoutParams relParentParam = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
        relParent.setLayoutParams(relParentParam);

        // Create child ImageView
        ImageView imgView = new ImageView(this);
        RelativeLayout.LayoutParams imgViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        imgView.setLayoutParams(imgViewParams);
        imgView.setImageResource(android.R.drawable.ic_menu_add);
        imgViewParams.addRule(RelativeLayout.CENTER_IN_PARENT);

        // Add child ImageView to parent RelativeLayout
        relParent.addView(imgView);

        // Set parent RelativeLayout to your screen
        setContentView(relParent, relParentParam);
    }

}

关于android - 是否可以在 Android 中创建没有 xml 的 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37240765/

相关文章:

android - 如何根据其状态设置 SearchView 的样式?

android - 如何使用导航组件切换到不同后台堆栈中的其他 fragment ?

java - Mediaplayer 停止播放我的声音 - Android studio

Android 启动画面计时器无法正常工作

java - 如何开发在首次启动时出现的演练对话框?

安卓性能 : Adding view programmatically vs setting view to GONE/VISIBLE

java - 将可绘制对象保存到解析中

android - ExoPlayer - 如何获取默认布局的源代码?

android - 是否可以从 webservice 的 xml 在 android 中创建布局?

android - 自定义首选项样式填充问题