从充气布局中删除黑色边框

时间:2014-12-18 20:56:38

标签: android android-layout

我正在使用PopupWindow,这工作正常,但问题是在使用黑色边框显示的布局视图膨胀之后。

final PopupWindow popupWindow = new PopupWindow(context);
LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pop_up_window_menu_layout, null);
popupWindow.setContentView(view);

pop_up_window_menu_layout.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/solid_white"
android:orientation="vertical" >

<TextView
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center_vertical"
        android:minHeight="?android:attr/listPreferredItemHeightSmall"
        android:text="SAVE"
        android:textAppearance="?android:attr/textAppearanceListItemSmall"
        android:textColor="@color/black"
        android:textSize="@dimen/txt_size_sliding_list" />

我尝试使用popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT))删除边框,但问题仍然存在。

有谁能告诉我如何删除不需要的黑色边框?

4 个答案:

答案 0 :(得分:4)

我的弹出式窗口周围没有任何边框,但我确实将它们设置得与你略有不同。希望这能指出你正确的方向。

popup_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:background="#f5f5f5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <TextView
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <Button
        android:id="@+id/btn_close"
        android:text="Close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:id="@+id/main_view"
                tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_popup"
        android:text="Popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <TextView
        android:id="@+id/txt_text"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends ActionBarActivity implements View.OnClickListener {

    View popupView;
    Button btnPopup;
    Button btnClose;
    PopupWindow popupWindow;

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

        popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
        btnClose = (Button) popupView.findViewById(R.id.btn_close);
        btnClose.setOnClickListener(this);

        popupWindow = new PopupWindow(popupView,
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);

        btnPopup = (Button) findViewById(R.id.btn_popup);
        btnPopup.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.btn_popup) {
            popupWindow.showAtLocation(findViewById(R.id.main_view), Gravity.NO_GRAVITY, 100, 200);
            //popupWindow.showAsDropDown(btnPopup, 10, 10);
        } else if (v.getId() == R.id.btn_close) {
            popupWindow.dismiss();
        }
    }
}

Screenshot

答案 1 :(得分:2)

如果您获得黑色边框,则表示已设置默认背景。

这是你删除它的方法:

popup.setBackgroundDrawable(null);

试试这个

答案 2 :(得分:1)

试试看,让我知道..

请务必创建引用自定义主题的弹出窗口:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="DialogTheme" parent="android:Theme.Dialog">
        <!-- Fill the screen -->
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">fill_parent</item>

        <!-- No backgrounds and No titles , and no Window Floating -->
        <item name="android:windowBackground">@null</item>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowIsFloating">false</item>      

        <!-- Set background what you want-->
        <item name="android:background">#ff0000</item>
    </style>

</resources>

答案 3 :(得分:1)

这是因为您在弹出窗口构造函数中发送上下文。 -

final PopupWindow popupWindow = new PopupWindow(context);
  

通过弹出窗口视图更改此内容 -

 View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
PopupWindow popupWindow = new Pop