膨胀后以编程方式将视图添加到LinearLayout

时间:2014-06-15 18:05:45

标签: android android-layout android-view layout-inflater android-popupwindow

我想将一些LinearLayouts添加到现有的Linearlayout。

活动的xml看起来如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/popupLinearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPopUp"
    android:gravity="center"
    android:orientation="vertical" >

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="none" >

        <LinearLayout
            android:id="@+id/ll_horizontal"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" >
        </LinearLayout>
    </HorizontalScrollView>

</LinearLayout>

活动代码:

public class MainActivity extends Activity {
    private Point p;
    private PopupWindow popup;
    private LinearLayout myLInearLayout;
    private TextView valueTV;
    private Button valueB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button popUpButton = (Button) findViewById(R.id.open);
        popUpButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (p != null)
                    showPopup(MainActivity.this, p);
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        int[] location = new int[2];
        Button button = (Button) findViewById(R.id.open);

        button.getLocationOnScreen(location);
        // Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];

    }

    // The method that displays the popup.

    private void showPopup(final Activity context, Point p) {

        Rect rectgle = new Rect();
        Window window = getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectgle);

        int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
        int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;

        // Inflate the popup_layout.xml
        LinearLayout viewGroup = (LinearLayout) context
                .findViewById(R.id.popupLinearLayout);
        LayoutInflater layoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);

        // Creating the PopupWindow
        popup = new PopupWindow(context);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setOutsideTouchable(true);
        popup.setFocusable(true);
        popup.setAnimationStyle(R.style.PopupWindowAnimation);

        // Some offset to align the popup a bit to the right, and a bit down,
        // relative to button's position.

        int OFFSET_X = 0;
        int OFFSET_Y = 0;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y
                + OFFSET_Y);

        // add LInearLayout
        myLInearLayout = (LinearLayout) findViewById(R.id.ll_horizontal);

        // add LayoutParams
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT);
        myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);

        // add textView
        valueTV = new TextView(this);
        valueTV.setText("The developer world is yours");
        valueTV.setId(5);
        valueTV.setLayoutParams(params);

        // add Button
        valueB = new Button(this);
        valueB.setText("thedeveloperworldisyours");
        valueB.setId(5);

        // add the textView and the Button to LinearLayout
        myLInearLayout.addView(valueTV);
        myLInearLayout.addView(valueB);
    }

    @Override
    public void onBackPressed() {
        if (popup != null && popup.isShowing()) {
            popup.dismiss();
            popup = null;
        } else {
            super.onBackPressed();
        }
    }

正如您所看到的,我希望在向视图充气并在PopupWindow中使用动画显示它之后添加Button和TextView。我想在ScrollView中为LinearLayout添加一些视图。这是出于测试目的,稍后我想将完整的LinearLayouts添加到ScrollView内的LinearLayout。 动画完美无缺。我只是无法以编程方式添加一些视图。我得到的所有东西都是Line中的NullPointerException,我尝试添加视图。

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

Try this way,hope this will help you to solve your problem.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button popUpButton = (Button) findViewById(R.id.open);
        int[] location = new int[2];
        popUpButton.getLocationOnScreen(location);
        // Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];
        popUpButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                if (p != null)
                    showPopup(p);
            }
        });

    }

    // The method that displays the popup_layout.

    private void showPopup(Point p) {

        Rect rectgle = new Rect();
        Window window = getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectgle);

        int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
        int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;

        // Inflate the popup_layout.xml

        LinearLayout viewGroup = (LinearLayout) findViewById(R.id.popupLinearLayout);
        View layout = LayoutInflater.from(this).inflate(R.layout.popup_layout, viewGroup);

        // Creating the PopupWindow
        popup = new PopupWindow(this);
        popup.setContentView(layout);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setOutsideTouchable(true);
        popup.setFocusable(true);

       // popup.setAnimationStyle(R.style.PopupWindowAnimation);

        // Some offset to align the popup_layout a bit to the right, and a bit down,
        // relative to button's position.

        int OFFSET_X = 0;
        int OFFSET_Y = 0;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // Displaying the popup_layout at the specified location, + offsets.
        popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);

        // add LInearLayout
        myLInearLayout = (LinearLayout) layout.findViewById(R.id.ll_horizontal);

        // add LayoutParams
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.MATCH_PARENT);
        myLInearLayout.setOrientation(LinearLayout.HORIZONTAL);

        // add textView
        valueTV = new TextView(this);
        valueTV.setText("The developer world is yours");
        valueTV.setId(5);
        valueTV.setLayoutParams(params);

        // add Button
        valueB = new Button(this);
        valueB.setText("thedeveloperworldisyours");
        valueB.setId(5);

        // add the textView and the Button to LinearLayout
        myLInearLayout.addView(valueTV);
        myLInearLayout.addView(valueB);
    }

答案 1 :(得分:0)

我想出了怎么做。我只需要膨胀popup.xml就可以获得我想要自定义的LinearLayout的引用。

以下是有人遇到同样问题的代码:

public class MainActivity extends Activity {
    private Point p;
    private PopupWindow popup;
    private Button popUpButton;
    private TextView valueTV;
    private Button valueB;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        popUpButton = (Button) findViewById(R.id.open);
        popUpButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (p != null)
                    showPopup(MainActivity.this, p);
            }
        });

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        int[] location = new int[2];
        Button button = (Button) findViewById(R.id.open);

        button.getLocationOnScreen(location);
        // Initialize the Point with x, and y positions
        p = new Point();
        p.x = location[0];
        p.y = location[1];

    }

    // The method that displays the popup.

    private void showPopup(final Activity context, Point p) {

        Rect rectgle = new Rect();
        Window window = getWindow();
        window.getDecorView().getWindowVisibleDisplayFrame(rectgle);

        int popupWidth = this.getResources().getDisplayMetrics().widthPixels;
        int popupHeight = this.getResources().getDisplayMetrics().heightPixels / 4;

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View contentView = inflater.inflate(R.layout.popup_layout, null);
        ViewGroup dlgView = (ViewGroup) contentView
                .findViewById(R.id.ll_horizontal);

        // Creating the PopupWindow
        popup = new PopupWindow(context);
        popup.setContentView(contentView);
        popup.setWidth(popupWidth);
        popup.setHeight(popupHeight);
        popup.setFocusable(true);
        popup.setAnimationStyle(R.style.PopupWindowAnimation);

        // Some offset to align the popup a bit to the right, and a bit down,
        // relative to button's position.

        int OFFSET_X = 0;
        int OFFSET_Y = 0;

        // Clear the default translucent background
        popup.setBackgroundDrawable(new BitmapDrawable());

        // add textView
        valueTV = new TextView(this);
        valueTV.setText("The developer world is yours");
        valueTV.setId(5);

        // add Button
        valueB = new Button(this);
        valueB.setText("thedeveloperworldisyours");
        valueB.setId(5);

        // add the textView and the Button to LinearLayout
        dlgView.addView(valueTV);
        dlgView.addView(valueB);

        // Displaying the popup at the specified location, + offsets.
        popup.showAtLocation(popUpButton, Gravity.NO_GRAVITY, p.x + OFFSET_X,
                p.y + OFFSET_Y);
    }
}