是否有可能在Android中创建没有xml的视图?
我想没有XML是否有可能在Android中创建没有xml的视图?
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);
}
}
这不回答问题。问题是关于创建一个自定义的'ViewGroup',供'WindowManager'使用。这不是关于创建一项活动。 – CommonsWare
好的,但标题要求创建没有XML的视图 – Harsh4789
答案需要回答问题,而不仅仅是与标题相关的邮政编码。 – CommonsWare
'WindowManager.LayoutParams'不适用于'Activity'中的'View's。我不确定你想要做什么,但是如果你希望你的自定义'View'作为整个布局,只需调用'setContentView()'。否则,将其添加到'Activity'布局中的'ViewGroup'。为了以'RelativeLayout.LayoutParams'为中心,你正在寻找'addRule()'方法。 –
另外,'ViewGroup'不知道布置其子项。要么实际实现自定义'ViewGroup'的其余代码(例如填充空的'onLayout()'方法),要么扩展其他具有特定布局规则的类(例如,扩展'RelativeLayout'而不是试图包装'RelativeLayout')。 – CommonsWare