How to invoke custom view on action bar button click?

时间:2016-12-09 12:57:35

标签: android android-layout android-actionbar android-view

I'm new in Android.

I need to draw custom view (or close if it exists) on search button click.

Now I implemented it this way:

public class CategoryActivity extends AppCompatActivity {

private FrameLayout relativeLayout;
private Toolbar myToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_demo);
    relativeLayout = (FrameLayout) findViewById(R.id.activity_demo);
    myToolbar = (Toolbar) findViewById(R.id.demo_toolbar);
    myToolbar.setTitle(R.string.main_page);
    setSupportActionBar(myToolbar);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.demo_activity_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    ...
    MyView myView = new MyView(this); //create vustom view
    int cX = relativeLayout.getWidth() / 2; //calculate coordinates
    int cY = relativeLayout.getHeight() / 2;
    FrameLayout frameLayout = new FrameLayout(this); //create new layout
    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(cX, cY); 
    layoutParams.setMargins(relativeLayout.getWidth() - timerX, relativeLayout.getPaddingTop() + myToolbar.getHeight(), 0, 0);
    frameLayout.setLayoutParams(layoutParams);
    frameLayout.addView(myView);
    relativeLayout.addView(frameLayout); 
    return super.onOptionsItemSelected(item);
    ...
}

}

It should look like this

What is right way to do it?

Thanks.

1 个答案:

答案 0 :(得分:0)

我完全不明白你的问题,因为你没有给出错误日志或显示你遇到的问题 我做了一个最简单的例子来显示如何动态添加视图。检查此示例,之后您可以询问您的方案。

layout:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_layout"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button
        android:id="@+id/btn_add_view"
        android:text="Add new view dynamically"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

代码:MainActivity.java

public class MainActivity extends AppCompatActivity {
    LinearLayout linearLayout;
    private Button btnAddView;
    int count = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
        btnAddView = (Button) findViewById(R.id.btn_add_view);
        btnAddView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                TextView tv = new TextView(MainActivity.this);
                ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tv.setLayoutParams(params);
                tv.setText("sample text" + count++);
                linearLayout.addView(tv);

            }
        });
    }
}

输出:

Output