我的布局包含一个LinearLayout
,其中包含一个按钮时。
通货膨胀之后,还会以编程方式添加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)
- 所以我无法找到实现这一目标的方法,除了嵌套以编程方式创建虚拟父布局中的视图 - 但我想尽可能避免这种情况。
答案 0 :(得分:0)
创建文件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)
}