gpt4 book ai didi

android - 膨胀成 "this"?

转载 作者:行者123 更新时间:2023-11-29 00:21:46 27 4
gpt4 key购买 nike

我一直在创建没有太多 XML 的应用程序,以编程方式创建 View 。我想切换到 XML。所以我为 RelativeLayout 编写了一个 XML 文件,我需要将它扩展到一个现有的类(当然是 RelativeLayout 的子类)中,该类具有所有实现逻辑。

我如何在构造函数中膨胀到“this”?

顺便问一下,XML 的真正优势是什么?当我在代码中创建 View 时,我缩放字体和图像,并根据屏幕的大小、方向、纵横比等移动 View 。使用 XML 方法,我必须为所有可能的配置创建一个单独的 XML。 .

构造函数代码:

  public OrderEditControl()
{
super(LmcActivity.W.getApplicationContext());
Resources res = LmcActivity.W.getResources();
setBackgroundColor(Color.TRANSPARENT);
headers = res.getStringArray(R.array.item_list_columns);
widths = new int[headers.length];

createLabels();
createButtons();

LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(ALIGN_PARENT_TOP);
lp.addRule(RIGHT_OF, labels[LabelType.CUSTOMER.ordinal()].getId());
lp.addRule(LEFT_OF, buttons[ButtonType.FIND_CUSTOMER.ordinal()].getId());

customerView = new TextView(LmcActivity.W.getApplicationContext());
customerView.setTextColor(Color.BLACK);
customerView.setId(400);
customerView.setTypeface(Typeface.DEFAULT_BOLD);
customerView.setGravity(Gravity.CENTER_VERTICAL);
addView(customerView, lp);

lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(ALIGN_TOP, labels[LabelType.SHIP_TYPE.ordinal()].getId());
lp.addRule(ALIGN_BOTTOM, labels[LabelType.SHIP_TYPE.ordinal()].getId());
lp.addRule(RIGHT_OF, labels[LabelType.SHIP_TYPE.ordinal()].getId());

shipSpinner = new Spinner(LmcActivity.W);
shipSpinner.setId(401);
shipSpinner.setAdapter(shipAdapter);
shipSpinner.setOnItemSelectedListener(this);
addView(shipSpinner, lp);

deliveryView = new EditText(LmcActivity.W.getApplicationContext());
deliveryView.setGravity(Gravity.CENTER_VERTICAL);
deliveryView.setSingleLine();
deliveryView.setId(402);
addView(deliveryView);

lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RIGHT_OF, labels[LabelType.COMMENTS.ordinal()].getId());
lp.addRule(LEFT_OF, buttons[ButtonType.ITEMS.ordinal()].getId());
lp.addRule(ALIGN_TOP, labels[LabelType.COMMENTS.ordinal()].getId());

commentView = new EditText(LmcActivity.W.getApplicationContext());
commentView.setGravity(Gravity.TOP);
commentView.setId(403);
addView(commentView, lp);

lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
lp.addRule(BELOW, commentView.getId());
itemList = new ListView(LmcActivity.W.getApplicationContext());
itemList.addHeaderView(createRow(null, null), null, false);
itemList.setOnItemClickListener(this);
itemList.setAdapter(itemAdapter);
itemList.setCacheColorHint(0);
itemList.setBackgroundColor(Color.TRANSPARENT);
itemList.setId(404);
addView(itemList, lp);

lays[0] = new LayParm(false);
lays[1] = new LayParm(true);
}

/** create the view's buttons */
private void createButtons()
{
for (int i = 0; i < N_BUT; ++i)
{
Button but = i == ButtonType.ITEMS.ordinal() ?
new TextGlassButton(2.4f, LmcActivity.W.getResources().getString(R.string.items), Color.WHITE) :
new EffGlassButton(1.2f, butEffects[i]);
but.setId(BUT_ID + i);
but.setOnClickListener(this);
buttons[i] = but;

if (i == ButtonType.DATE.ordinal())
addView(but);
else
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if (i < 2)
lp.addRule(ALIGN_PARENT_TOP);
else
lp.addRule(BELOW, BUT_ID + i - 2);

if (i % 2 == 0)
lp.addRule(ALIGN_PARENT_RIGHT);
else
lp.addRule(LEFT_OF, BUT_ID + i - 1);

addView(but, lp);
}
}
}

/** create text labels */
private void createLabels()
{
Paint paint = AFDraw.W.textPaint;
paint.setTextSize(Universe.TEXT_SIZE);
paint.setTypeface(LmcActivity.W.defaultTypeface);

String[] titles = LmcActivity.W.getResources().getStringArray(R.array.order_labels);

for (int i = 0; i < titles.length; ++i)
{
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(ALIGN_PARENT_LEFT);

if (i == 0)
lp.addRule(ALIGN_PARENT_TOP);
else
lp.addRule(BELOW, LABEL_ID + i - 1);

TextView tv = new TextView(LmcActivity.W.getApplicationContext());
tv.setText(titles[i]);
tv.setTextColor(Color.BLACK);
tv.setId(LABEL_ID + i);
tv.setTypeface(LmcActivity.W.defaultTypeface);
tv.setGravity(Gravity.CENTER_VERTICAL);
labels[i] = tv;
addView(tv, lp);

labelWidth = Math.max(labelWidth, paint.measureText(titles[i]));
}

labelWidth += Universe.TEXT_SIZE * 0.5f;
dateWidth = paint.measureText("00/00/00") + Universe.TEXT_SIZE * 1.5f;
}

最佳答案

@scriptocalypse 通常是正确的,但是子类化一些布局并将自定义布局扩展到此类有助于分离不同的抽象。有那么多糟糕的教程,其中的所有内容都在 Activity 中完成。我看到世界上新来的程序员只会编写看起来很垃圾的应用程序。

使用自定义布局,您只能在 Activity 中执行以下操作:

medicineView.putMedicine(medicineList);

而不是所有蹩脚的适配器创建和寻找 View ......

首先您应该为自定义 View 创建一些 View :

<RelativeLayout ...>
<!-- You put all your views here -->
</RelativeLayout>

其次如果您对您的 View 感到满意,您应该将根更改为merge 标签:

<merge ...>
<!-- You put all your views here -->
</merge>

这很重要。我们从 RelativeLayout 标签开始设计,以便 IDE 知道如何绘制布局,以及如何完成。但是如果我们让它保持原样,我们将在两个嵌套的 RelativeLayouts 中结束,它最终会是这样的:

<RelativeLayout ...>    <!-- That is your class -->
<RelativeLayout ...> <!-- This is inflated from layout -->
<!-- You put all your views here -->
</RelativeLayout>
</RelativeLayout>

如果您将布局更改为“合并”,那么它将如下所示:

<RelativeLayout ...>    <!-- That is your class -->
<merge...> <!-- This is inflated from layout -->
<!-- You put all your views here -->
</merge>
</RelativeLayout>

并将合并到它的根:

<RelativeLayout ...>    <!-- That is your class, merged with layout -->
<!-- You put all your views here -->
</RelativeLayout>

最后您必须对要求的 View 或 ViewGroup 进行子类化:

public class CustomView extends RelativeLayout {
public CustomView(Context context) {
super(context);
initialize();
}

public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initialize();
}

public CustomView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initialize();
}

private void initialize() {
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.id.your_layout, this, true);

// find your views, set handlers etc.
}
}

用法

就像@scriptocalypse 已经说过的那样。在另一种布局中,您可以这样使用它:

<SomeLayout>
<com.foo.CustomView>
</SomeLayout>

关于android - 膨胀成 "this"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22523232/

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