繁体   English   中英

如何从动态视图获取ID并为其启动上下文菜单?

[英]How get id from dynamic view and start for them context menu?

我创建用于添加任何TextView的LinearLayout的类:

public class Tabs extends LinearLayout {


Configuration c = getResources().getConfiguration();

int screenWidthDP = c.screenWidthDp;
float density = this.getResources().getDisplayMetrics().density;

float ftabsize = screenWidthDP * density / 3;
int tabsize = (int) (float) ftabsize;

float fTabMargins = 1 * density;
int tabMargins = (int) (float) fTabMargins;

LinearLayout tabB;
LayoutParams tabBP;
LinearLayout tab;
LayoutParams tabP;
TextView text;
LayoutParams textP;

Tabs(Context context, String name, int id) {
    super(context);

//tabB
    tabB = new LinearLayout(context);
    tabB.setOrientation(HORIZONTAL);
    tabBP = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
    tabBP.width = tabsize;
    tabBP.setMargins(tabMargins,tabMargins,tabMargins,tabMargins);
    tabB.setLayoutParams(tabBP);
    tabB.setBackgroundColor(Color.BLACK);
    tabB.setGravity(Gravity.CENTER);

    //tab

    tab = new LinearLayout(context);
    tab.setOrientation(HORIZONTAL);
    tabP = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    tabP.setMargins(4,4,4,4);
    tab.setLayoutParams(tabP);
    tab.setBackgroundColor(Color.WHITE);
    tab.setGravity(Gravity.CENTER);
    tab.setId(id);
    tabB.addView(tab);


    //Text in Tab
    text = new TextView(context);
    textP = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    text.setLayoutParams(textP);
    text.setText(name);
    /*text.setText(String.valueOf(ress));*/

    tab.addView(text);

}
public void addTab(LinearLayout ll) {ll.addView(tabB);}}

并将此类添加到另一个LinearLayout中:

public class MainActivity extends AppCompatActivity {

final String LOG_TAG = "myLogs";
final String SqlInit1 = "INSERT INTO tabs " +
        "(" +
        "name, " +
        "color, " +
        "order1" +
        ")" +
        "VALUES(" +
        "'first', " +
        "'blue'," +
        "'1'" +
        ");";
final String SqlInit2 = "INSERT INTO tabs " +
        "(" +
        "name, " +
        "color, " +
        "order1" +
        ")" +
        "VALUES(" +
        "'second'," +
        "'red', " +
        "'2'" +
        ");";
DBHelper dbHelper;
LinearLayout n77;
int rrr;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LinearLayout ll = (LinearLayout) findViewById(R.id.tabs);


    dbHelper = new DBHelper(this);
    SQLiteDatabase db = dbHelper.getReadableDatabase();
    Log.d(LOG_TAG, "Reading Database....");
    Cursor c = db.query("tabs", null, null, null, null, null, null);
    if (c.moveToFirst()) {
        int idColIndex = c.getColumnIndex("id");
        int nameColIndex = c.getColumnIndex("name");
        int colorColIndex = c.getColumnIndex("color");
        int order1ColIndex = c.getColumnIndex("order1");

        do {
            Tabs myTab = new Tabs(this, c.getString(nameColIndex), c.getInt(idColIndex));
            myTab.addTab(ll);
            registerForContextMenu(myTab);
            if (c.getString(nameColIndex) == "first") {
                rrr = myTab.gID();
            }

          Log.d(LOG_TAG, "ID = " + c.getInt(idColIndex) + "\n" +
                  "Name = " + c.getString(nameColIndex) + "\n" +
                  "Color = " + c.getString(colorColIndex) + "\n" +
                  "Order = " + c.getString(order1ColIndex));
        } while (c.moveToNext());
    } else
        Log.d(LOG_TAG, "0 Rows...");
    c.close();

    dbHelper.close();
    Log.d(LOG_TAG, "Closing database....");


    n77 = (LinearLayout) findViewById(R.id.n77);
    registerForContextMenu(n77);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
  /*  super.onCreateContextMenu(menu, v, menuInfo);*/

    switch (v.getId()) {
        case R.id.n77:
            menu.setHeaderTitle("Title:");
            menu.add(0,1,0,"1");
            menu.add(0,2,0,"2");
            break;
       /* case rrr:
            menu.add(0,1,0,"3");
            break;*/

    }
}


class DBHelper extends SQLiteOpenHelper {
   ...
}}

如何获取LinearLayout的ID(从数据库信息创建)并在onCreateContextMenu中使用它?

 tab.getChildAt(index).getId()

这将为您提供在特定索引处动态添加的视图ID。

我遇到了新的麻烦:在这种结构中:

if (v.getId() == ll.getChildAt(0).getId()) {
        menu.setHeaderTitle("First Title");
        menu.add(0, v.getId(), 0, "Zero.");
    } else if (v.getId() == ll.getChildAt(1).getId()) {
        menu.setHeaderTitle("Second Title");
        menu.add(0, v.getId(), 0, "One")));
}

如果在getChildAt(0)或getChildAt(1)上打开上下文菜单,则存在相同的菜单项:“零”和​​“一个”。 但是我不会为这两个视图提供单独的菜单。 如果R.id.xxxx使用了开关操作符,则两个视图都有单独的菜单。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM