草庐IT

Android:对话框最小边距

coder 2023-11-22 原文

我使用 android android.app.Dialog 创建自定义对话框(用于按钮和背景) 在我的对话框中,我在 ScrollView 中有一个 TextView,而我有一个简短的文本,这完美地显示了我想要的方式,但如果文本非常大,我的 Dialog 会占据全屏我希望对话框和屏幕边缘之间的边距最小。

我的问题是我希望对话框不要比需要的大,而且我不能为此设置固定大小?

这是今天的样子.....我有这样的边距


GameDialog.java

public class GameDialog extends Dialog {

    public GameDialog(Context ct, int titleID, int messageID) {
        super(ct, R.style.dialog_style);
        this.setContentView(R.layout.dialog_layout);

    }
    // This exist more code but this has noting with the layout to do,only set the text and show the button that exist since the XML file.

}

R.style.dialog_style

<style name="dialog_style" parent="@android:style/Theme.Dialog">
    <item name="android:windowBackground">?button_image</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:textColor">#FF000000</item>
    <item name="android:textSize">20sp</item>
</style>

R.layout.dialog_layout

<?xml version="1.0" encoding="utf-8"?>


<!-- Dialog layout that show title and a textarea, and under this allow a row of button that is center layout. -->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

    <LinearLayout android:orientation="vertical" 
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_marginLeft="14px"
                  android:layout_marginRight="14px">


        <TextView
            android:layout_gravity="center_vertical"    
            android:id="@+id/text_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content">
        </TextView>

    </LinearLayout>

    <LinearLayout android:orientation="vertical"    
                  android:layout_width="fill_parent"
                  android:layout_height="0px"
                  android:layout_weight="1"
                  android:layout_marginLeft="14px"
                  android:layout_marginRight="14px">

        <ScrollView 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
        >

            <TextView android:id="@+id/text_main"
                      android:padding="5px"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
            >
            </TextView>

        </ScrollView>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/layout_button"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:orientation="horizontal">
            <!-- this will be show while need to show a button -->
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
        <ImageView
            style="?icon_size.button"
            android:visibility="gone"/>
    </LinearLayout>
</LinearLayout> 

最佳答案

框架实际上是在背景图像本身上执行此操作。换句话说,如果您查看为框架定义的主题,它们的 windowBackground属性只是一种清晰的颜色,即:

<item name="android:windowBackground">@android:color/transparent</item>

然后放置在根布局项上的背景图像是一个 9 block 补丁,在所有边上都有内置填充(即图像中内置的额外空间,因此当图像拉伸(stretch)以填满屏幕时您仍然可以看到周围边缘)。查看 AOSP 源代码中的一个面板,了解我的意思 ( link )。

因此,对于您的解决方案,我会推荐两件事:

  1. 使用 windowBackground 修改您的自定义样式如上所述
  2. 更新您的自定义背景图片以包含固有的透明边距

对于第 2 步,如果您的图像当前是 9 补丁,请执行与 Google 相同的操作。如果您使用 XML 创建图像,则可以使用 <layer-list>像下面的示例一样,将可见矩形嵌入另一个带有填充的矩形中:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="@android:color/transparent" />
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>
    <!-- This shape will be inset by the padding set above -->
    <item>
        <shape android:shape="rectangle" >
            <solid android:color="#262626" />
        </shape>
    </item>
</layer-list>

在这种情况下,填充不会转移到内容,因此您还需要向根布局项添加填充,以使内容边界与图像显示相匹配。

关于Android:对话框最小边距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13298617/

有关Android:对话框最小边距的更多相关文章

  1. 安卓apk修改(Android反编译apk) - 2

    最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路

  2. ruby - 获取数组中的值并最小化某个类属性的最优雅的方法是什么? - 2

    假设我有以下类(class):classPersondefinitialize(name,age)@name=name@age=ageenddefget_agereturn@ageendend我有一组Person对象。是否有一种简洁的、类似于Ruby的方法来获取最小(或最大)年龄的人?如何根据它对它们进行排序? 最佳答案 这样做会:people_array.min_by(&:get_age)people_array.max_by(&:get_age)people_array.sort_by(&:get_age)

  3. ruby - 返回空白页的最小 Capybara/Poltergeist 测试 - 2

    看来我正在回顾SO帖子中采取的步骤:Capybara,PoltergeistandPhantomjsandgivinganemptyresponseinbody.(如果你愿意,可以将其标记为重复,但我包含了一个最小的独立测试用例和版本号。)问题我做错了什么吗?我可以运行另一个可能有助于隔离问题的最小测试吗?文件:pgtest.rbrequire'rubygems'require'capybara'require'capybara/dsl'require'capybara/poltergeist'modulePGTestincludeCapybara::DSLextendselfdeft

  4. ruby - 寻找产品和商店的最佳组合以最小化成本的算法 - 2

    你好,Stackoverflow的人们,我经营一个网站,为用户寻找最便宜的书籍购买地点。这对于单本书来说很容易,但对于多本书来说,有时在一家商店购买一本书而在另一家商店购买另一本书会更便宜。目前我找到了销售用户列表中所有书籍的最便宜的商店,但我想要一个更智能的系统。这里有更多信息:一本书的价格对于一家商店来说是不变的。运费可能会有所不同,具体取决于书籍的数量或书籍的总值(value)。每个商店对象都可以获取一组书籍并返回运费。通常,并非每家书店都出售每一本书。不确定在这里链接到我的站点是否很酷,但它列在我的用户配置文件中。我希望能够找到最便宜的商店和书籍组合。我担心这需要一种蛮力方法-

  5. Android Studio开发之使用内容组件Content获取通讯信息讲解及实战(附源码 包括添加手机联系人和发短信) - 2

    运行有问题或需要源码请点赞关注收藏后评论区留言一、利用ContentResolver读写联系人在实际开发中,普通App很少会开放数据接口给其他应用访问。内容组件能够派上用场的情况往往是App想要访问系统应用的通讯数据,比如查看联系人,短信,通话记录等等,以及对这些通讯数据及逆行增删改查。首先要给AndroidMaifest.xml中添加响应的权限配置 下面是往手机通讯录添加联系人信息的例子效果如下分成三个步骤先查出联系人的基本信息,然后查询联系人号码,再查询联系人邮箱代码 ContactAddActivity类packagecom.example.chapter07;importandroid

  6. Android 10.0 设置默认launcher后安装另外launcher后默认Launcher失效的功能修复 - 2

    1.前言 在10.0的系统rom定制化开发中,在系统中有多个launcher的时候,会在开机进入launcher的时候弹窗launcher列表,让用户选择进入哪个launcher,这样显得特别的不方便所以产品开发中,要求用RoleManager的相关api来设置默认Launcher,但是在设置完默认Launcher以后,在安装一款Launcher的时候,默认Launcher就会失效,在系统设置的默认应用中Launcher选项就为空,点击home键的时候会弹出默认Launcher列表,让选择进入哪个默认Launcher.所以需要从安装Launcher的流程来分析相关的设置。来解决问题设置默认La

  7. AiBote 2022 新研发的自动化框架,支持 Android 和 Windows 系统。速度非常快 - 2

    Ai-Bot基于流行的Node.js和JavaScript语言的一款新自动化框架,支持Windows和Android自动化。1、Windowsxpath元素定位算法支持支持Windows应用、.NET、WPF、Qt、Java和Electron客户端程序和ie、edgechrome浏览器2、Android支持原生APP和H5界面,元素定位速度是appium十倍,无线远程自动化操作多台安卓设备3、基于opencv图色算法,支持找图和多点找色,1080*2340全分辨率找图50MS以内4、内置免费OCR人工智能技术,无限制获取图片文字和找字功能。5、框架协议开源,除官方node.jsSDK外,用户可

  8. Android Gradle 7.1+新版本依赖变化 - 2

    前一段时间由于工作需要把可爱的小雪狐舍弃了,找到了小蜜蜂。但是新版本的小蜜蜂出现了很多和旧版本不一样的位置。1.功能位置迁移,原来在工程build.gradle的buildscript和allprojects移动至setting.gradle并改名为pluginManagement和dependencyResolutionManagement。里面的东西依旧可以按照原来的copy过来。pluginManagement{repositories{gradlePluginPortal()google()mavenCentral()}}dependencyResolutionManagement{r

  9. ruby - Ruboto 的最佳教程(适用于 Android 的 ruby​​)? - 2

    关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于StackOverflow来说是偏离主题的,因为它们往往会吸引自以为是的答案和垃圾邮件。相反,describetheproblem以及迄今为止为解决该问题所做的工作。关闭9年前。Improvethisquestion我几乎用完了Ruby,但现在想试试Ruboto,android上的ruby​​。谷歌未能给我足够的(几乎没有结果)。所以任何人都可以分享一些关于Ruboto的教程。

  10. 用于进行线性或非线性最小二乘近似的 Ruby 库? - 2

    是否有Ruby库允许我对一组数据进行线性或非线性最小二乘法逼近。我想做的是:给定一系列[x,y]数据点针对该数据生成线性或非线性最小二乘法近似值库不必弄清楚它是否需要进行线性或非线性近似。库的调用者应该知道他们需要什么类型的回归我不想尝试移植某些C/C++/Java库来获得此功能,因此我希望有一些现有的Ruby库可供我使用。 最佳答案 尝试使用“statsample”gem。您可以使用下面提供的示例执行对数、指数、幂或任何其他转换。我希望这有帮助。require'statsample'#IndependentVariablex_da

随机推荐