Android 在以编程方式创建的视图上设置 NextFocusDown
Android setNextFocusDown on programatically created view
我的布局包含一个 LinearLayout
,其中在展开时包含一个按钮。
在 inflation 之后,又以编程方式添加了 3 个视图,我希望能够做的是将初始按钮的 nextFocusDown
设置为循环中第一个创建的视图,就像这样:
ImageButton btnBack = (ImageButton) this.findViewById(btnBack);
forV(x=0;x<=45;x++) {
LayoutInflater inflater = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.my_great_item, null);
// what I really want to be able to do here is something like
// if(x==0) btnBack.setNextFocusDown(l);
// but here there only exists the method setNextFocusDownId - I can't pass a view directly to the function.
l.setFocusable(true);
l.setClickable(true);
...
l.setOnFocusChangeListener(new View.OnFocusChangeListener() {
...
});
l.setOnClickListener(new View.OnClickListener() {
...
});
mybaseview.addView(l)
}
问题是,根据评论,似乎没有方法 setNextFocusDown(View)
- 所以除了将以编程方式创建的视图嵌套在虚拟父布局 - 但我想尽可能避免这种情况。
创建文件values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="your_custom_id" type="id"/>
</resources>
比这样更改代码:
ImageButton btnBack = (ImageButton) this.findViewById(btnBack);
for(x=0;x<=45;x++) {
LayoutInflater inflater = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.my_great_item, null);
if (x==0) {
l.setId(R.id.your_custom_id);
btnBack.setNextFocusDown(R.id.your_custom_id);
}
l.setFocusable(true);
l.setClickable(true);
...
l.setOnFocusChangeListener(new View.OnFocusChangeListener() {
...
});
l.setOnClickListener(new View.OnClickListener() {
...
});
mybaseview.addView(l)
}
我的布局包含一个 LinearLayout
,其中在展开时包含一个按钮。
在 inflation 之后,又以编程方式添加了 3 个视图,我希望能够做的是将初始按钮的 nextFocusDown
设置为循环中第一个创建的视图,就像这样:
ImageButton btnBack = (ImageButton) this.findViewById(btnBack);
forV(x=0;x<=45;x++) {
LayoutInflater inflater = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.my_great_item, null);
// what I really want to be able to do here is something like
// if(x==0) btnBack.setNextFocusDown(l);
// but here there only exists the method setNextFocusDownId - I can't pass a view directly to the function.
l.setFocusable(true);
l.setClickable(true);
...
l.setOnFocusChangeListener(new View.OnFocusChangeListener() {
...
});
l.setOnClickListener(new View.OnClickListener() {
...
});
mybaseview.addView(l)
}
问题是,根据评论,似乎没有方法 setNextFocusDown(View)
- 所以除了将以编程方式创建的视图嵌套在虚拟父布局 - 但我想尽可能避免这种情况。
创建文件values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="your_custom_id" type="id"/>
</resources>
比这样更改代码:
ImageButton btnBack = (ImageButton) this.findViewById(btnBack);
for(x=0;x<=45;x++) {
LayoutInflater inflater = (LayoutInflater) _c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout l = (LinearLayout) inflater.inflate(R.layout.my_great_item, null);
if (x==0) {
l.setId(R.id.your_custom_id);
btnBack.setNextFocusDown(R.id.your_custom_id);
}
l.setFocusable(true);
l.setClickable(true);
...
l.setOnFocusChangeListener(new View.OnFocusChangeListener() {
...
});
l.setOnClickListener(new View.OnClickListener() {
...
});
mybaseview.addView(l)
}