gpt4 book ai didi

android - LayoutInflater 为什么从 xml 返回原始 View 的副本以及如何从 xml 获取原始 View

转载 作者:行者123 更新时间:2023-11-30 02:48:04 26 4
gpt4 key购买 nike

我想从 xml 文件获取一个 View 到普通类,我使用了 LayoutInflater,但它只给我这个 View 的副本而不是原始 View (我想设置对元素的更改)。

是否可以从 xml 获取原始布局?我该如何解决?

我想在其中获取布局的类

    public class Board  {
private Context context;
private View inflated;
private RelativeLayout screen;

public Board(Context context) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflated = inflater.inflate(R.layout.board, null);

screen = (RelativeLayout) inflated.findViewById(R.id.custom_board_screen);
this.context = context;
}

public TrippleToggleButton getButton(int id) {
//return (TrippleToggleButton) inflated.findViewById(id);
TrippleToggleButton btn;
for(int i=0;i<screen.getChildCount();i++)
{
View v1 = screen.getChildAt(i);
if(v1 instanceof TableLayout)
{
for(int j=0;j<((TableLayout) v1).getChildCount();j++)
{
View v2 = ((TableLayout) v1).getChildAt(j);
if (v2 instanceof TableRow)
{
for(int k=0;k<((TableRow) v2).getChildCount();k++)
{
View v3 = ((TableRow) v2).getChildAt(k);
if (v3 instanceof TrippleToggleButton)
{
Log.i("2014.07.10",screen.getId()+" ; "+v3.getId());
//originally id of button are 0-8; inflated 9 - 17
if (v3.getId()==id) return (TrippleToggleButton)v3;
}
}
}
}
}
}
return null;
}
}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_board_screen"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableLayout
android:id="@+id/custom_board_table"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true">

<TableRow
android:id="@+id/custom_board_table_row1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<View style="@style/DividingHorizontalLine"/>
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<board.TrippleToggleButton
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff000000">
<View style="@style/DividingHorizontalLine"/>
</TableRow>

<TableRow
android:id="@+id/custom_board_table_row2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<View style="@style/DividingHorizontalLine"/>
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<board.TrippleToggleButton
/>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff000000">
<View style="@style/DividingHorizontalLine"/>
</TableRow>

<TableRow
android:id="@+id/custom_board_table_row3"
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<View style="@style/DividingHorizontalLine"/>
<board.TrippleToggleButton
/>
<View style="@style/DividingVerticalLine"/>
<board.TrippleToggleButton
/>
</TableRow>




</TableLayout>

</RelativeLayout>

我的自定义按钮类:

public class TrippleToggleButton extends Button implements View.OnClickListener {
private Sign actual_state;
private final float scale = getContext().getResources().getDisplayMetrics().density;
private int dps = 50;
private int pixels = (int) (dps * scale + 0.5f);
private static int buttonCount = 0;

public TrippleToggleButton(Context context) {
this(context, null);
}

public TrippleToggleButton(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

public TrippleToggleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
addFeatures();
}

private void addFeatures() {
actual_state = Sign.UNCHECKED;
setOnClickListener(this);
ViewGroup.LayoutParams param = new ViewGroup.LayoutParams(pixels, pixels);
setLayoutParams(param);
setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
setMinimumHeight(pixels);
setMinimumWidth(pixels);
setMinHeight(pixels * 2);
setMinWidth(pixels * 2);
setPadding(pixels / 10, pixels / 10, pixels / 10, pixels / 10);
setTextSize(pixels);
setId(IDAllocator.getNextID()); // this is just a static method which sets ID's starting from 0
}

private void onUncheckedState() {

}

public Sign getActual_state() {
return actual_state;
}

public boolean isUnchecked() {
return getActual_state() == Sign.UNCHECKED;
}

public void setState(Sign newState) {
actual_state = newState;
setText(actual_state.toString());
setClickable(false);
}

@Override
public void onClick(View v) {
TrippleToggleButton btn = (TrippleToggleButton) v;

if (btn.isUnchecked()) {
Toast t = Toast.makeText(getContext(),""+getId(),Toast.LENGTH_SHORT); // it shows correct ID, everything goes ok here
t.show();
Container.performAction(btn);

}
}
}

最佳答案

从 xml 的角度来看,没有“原始”。 xml 描述一个布局,它并不是字面上的布局层次结构。每当您扩充 xml 时,扩充器都会使用 xml 作为构建新布局层次结构的指南,您可以引用它。如果您想更新那个原始的膨胀 View ,请将该原始引用传递给您的 Board 类并在那里执行更改,而不是再次膨胀整个事物。

关于android - LayoutInflater 为什么从 xml 返回原始 View 的副本以及如何从 xml 获取原始 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24687481/

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