Java Android-对话框模式视图中出现错误
我向你描述我的情况: 目标: 我想要以对话框模式打开的活动,该活动的布局仅在我设置Imageview、listview、button和textview的屏幕的一半中 问题: 我看到所有的布局,也空了一半,我还想删除顶部的标签 这是一个描述我的问题的屏幕截图: 我只想留下黄色矩形的区域,去掉另一部分 这是我的代码: Activity_main.xmlJava Android-对话框模式视图中出现错误,java,android,view,modal-dialog,label,Java,Android,View,Modal Dialog,Label,我向你描述我的情况: 目标: 我想要以对话框模式打开的活动,该活动的布局仅在我设置Imageview、listview、button和textview的屏幕的一半中 问题: 我看到所有的布局,也空了一半,我还想删除顶部的标签 这是一个描述我的问题的屏幕截图: 我只想留下黄色矩形的区域,去掉另一部分 这是我的代码: Activity_main.xml <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:and
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="83dp"
android:id="@+id/b1" />
</RelativeLayout>
Main2Activity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,Main2Activity.class);
startActivity(i);
}
});
}
}
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.macbookpro.provafragment">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main2Activity"
android:label=""
android:theme="@style/Theme.AppCompat.Light.Dialog" />
</application>
</manifest>
进入activity_main2.xml,但结果如下所示activity您可以在values/styles.xml中定义自定义样式,如下所示:
<style name="Theme.ActivityDialog" parent="android:style/Theme.AppCompat.Light.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:background">@android:color/transparent</item>
</style>
@空的
真的
真的
真的
@android:彩色/透明
并使用它AndroidManifest.xml:
<activity android:name=".Main2Activity"
android:label=""
android:theme="@style/Theme.ActivityDialog" />
主要问题是id=“centerShim”的视图似乎占据了所有高度。将此线添加到中心垫片视图
android:layout_over=“@+id/Button”当您的对话框打开时,图像中附加的是屏幕。在对话框中,上部的白色部分是对话框的标题栏。 如果要删除此标题栏,请在Main2活动中setContentView()之前写下这一行
requestWindowFeature(Window.FEATURE_NO_TITLE);
您的对话框将以全屏模式打开。这是因为在activity_main2.xml中将父布局的高度设置为“match_parent”
您的activity_main2.xml应该如下所示
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/centerShim5"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/textview2"
android:id="@+id/imageView"
android:layout_alignRight="@+id/centerShim"
android:layout_alignEnd="@+id/centerShim"
android:background="@android:color/holo_red_dark" />
<View
android:id="@+id/centerShim"
android:layout_height="match_parent"
android:layout_width="0dp"
android:visibility="invisible"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/textview2"
android:layout_toEndOf="@+id/textview2"
android:layout_above="@+id/Button"
android:layout_below="@+id/textview1"
android:background="@android:color/holo_blue_light">
</ListView>
<TextView
android:text=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:textStyle="normal|bold"
android:textAllCaps="true"
android:textAlignment="center"
android:textSize="14sp"
android:layout_above="@+id/Button"
android:layout_alignRight="@+id/centerShim"
android:layout_alignEnd="@+id/centerShim"
android:layout_marginBottom="13dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:textStyle="normal|bold"
android:textAllCaps="true"
android:textAlignment="center"
android:textSize="15dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/centerShim"
android:layout_toEndOf="@+id/centerShim" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview3"
android:textStyle="normal|bold"
android:textAllCaps="true"
android:textSize="15dp"
android:layout_above="@+id/Button"
android:layout_alignRight="@+id/centerShim"
android:layout_alignEnd="@+id/centerShim" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button"
android:elevation="11dp"
android:layout_above="@+id/centerShim5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
这可能是一个解决方案,但通过这种方式,我通过透明布局来解决我的问题,但我一直都是这样,因为当我在屏幕的后半部分单击时,对话框活动没有关闭。您是否尝试过为第二个活动将宽度和高度设置为“包裹内容”?是,问题始终存在。id=“centerShim”的视图似乎占据了所有高度。将这一行添加到中间垫片视图android:layout_over=“@+id/Button”不,我试过了,但不起作用,我用视图的屏幕截图编辑了我的提问我试过用你的代码,但我也有同样的问题。使用requestWindowFeature(Window.FEATURE\u NO\u TITLE)代码>标题栏不会消失,使用activity_main2.xml,我有一个与以前相同大小的全白色对话框在这种情况下,您最好尝试使用DialogFragment或custom dialog打开对话框参考DialogFragment和Android中的custom dialog示例,但问题总是一样的,我不明白为什么,我尝试不同的布局,所有的作品,但当我使用我的,不要。
requestWindowFeature(Window.FEATURE_NO_TITLE);
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="@+id/centerShim5"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:visibility="invisible" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/textview2"
android:id="@+id/imageView"
android:layout_alignRight="@+id/centerShim"
android:layout_alignEnd="@+id/centerShim"
android:background="@android:color/holo_red_dark" />
<View
android:id="@+id/centerShim"
android:layout_height="match_parent"
android:layout_width="0dp"
android:visibility="invisible"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/ListView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="@+id/textview2"
android:layout_toEndOf="@+id/textview2"
android:layout_above="@+id/Button"
android:layout_below="@+id/textview1"
android:background="@android:color/holo_blue_light">
</ListView>
<TextView
android:text=""
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview2"
android:textStyle="normal|bold"
android:textAllCaps="true"
android:textAlignment="center"
android:textSize="14sp"
android:layout_above="@+id/Button"
android:layout_alignRight="@+id/centerShim"
android:layout_alignEnd="@+id/centerShim"
android:layout_marginBottom="13dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview1"
android:textStyle="normal|bold"
android:textAllCaps="true"
android:textAlignment="center"
android:textSize="15dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/centerShim"
android:layout_toEndOf="@+id/centerShim" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textview3"
android:textStyle="normal|bold"
android:textAllCaps="true"
android:textSize="15dp"
android:layout_above="@+id/Button"
android:layout_alignRight="@+id/centerShim"
android:layout_alignEnd="@+id/centerShim" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Button"
android:elevation="11dp"
android:layout_above="@+id/centerShim5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>