0

我想将一些线性布局添加到现有的线性布局中。

Activity 的 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();
        }
    }

如您所见,我想在为 View 充气并在 PopupWindow 中用动画显示它之后添加一个 Button 和一个 TextView。我想在 ScrollView 内的 LinearLayout 中添加一些视图。这是出于测试目的,稍后我想将完整的 LinearLayouts 添加到 ScrollView 内的 LinearLayout。动画完美运行。我只是不能以编程方式添加一些视图。我得到的一切都是我尝试添加视图的行中的 NullPointerException。

我感谢您的帮助。

4

2 回答 2

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);
    }
于 2014-06-16T04:42:44.503 回答
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);
    }
}
于 2014-06-16T20:43:54.537 回答