简体   繁体   English

打开customdialog时为什么不显示`text`?

[英]Why won't `text` show when customdialog is opened?

I feel like an idiot. 我觉得自己像个白痴。 I copied this project from here . 我从这里复制了这个项目。 The zip file wouldn't import into Android Studio 1.0.2. 该zip文件不会导入Android Studio 1.0.2。 Advice was to migrate it to Gradle, but I don't know how to do so. 建议是将其迁移到Gradle,但我不知道该怎么做。 (I later found a link to doing so. I couldn't implement it. It's at the bottom of this mess.) (我后来找到了这样做的链接。我无法实现它。它处于这个混乱的底部。)

So I created a new project and cut-and-pasted the 3 xml and 1 java file. 所以我创建了一个新项目并剪切并粘贴了3 xml和1个java文件。 I finally got compilation. 我终于得到了汇编。

Supposedly the dialog below will be shown, but when I run it, it doesn't show the text of text , which is " Android custom dialog example ". 据说下面的对话框会显示出来,但是当我运行它时, 它不显示文本text ,这是“ Android自定义对话框示例 ”。 It just shows the icon; 它只显示图标; no text at all to its right as specified in custom.xml . custom.xml指定的文本根本没有。 I have spent hours (I CLEARLY do not have a good grasp of Android java or xml or the connection--I'm working on it--but I see what I expect to see in the java and xml for the TextView named text ) trying to fix this. 我花了好几个小时(我很清楚没有掌握Android java或xml或连接 - 我正在研究它 - 但我看到我期望在java和xml中看到TextView命名text )试图解决这个问题。 I am now hoping you all will give me a hand. 我现在希望你们都能帮助我。

What I have tried (in vain) is listed below the custom.xml file. 我尝试过的(徒劳)列在custom.xml文件下面。

在此输入图像描述

EDIT--HERE'S WHAT I DO SEE: 编辑 - 这是我看到的:

在此输入图像描述 This is AndroidManifest.xml : 这是AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.dslomer64.android">

    <application android:allowBackup="true"
                 android:label="@string/app_name"
                 android:icon="@drawable/ic_launcher"
                 android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

This is MainActivity.java : 这是MainActivity.java

package com.dslomer64.android;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class MainActivity extends Activity {

    final Context context = this;
    private Button button;

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.buttonShowCustomDialog);

        // add button listener
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // custom dialog
                final Dialog dialog = new Dialog(context);
                dialog.setContentView(R.layout.custom);
                dialog.setTitle("Title...");

                // set the custom dialog components - text, image and button
                TextView text = (TextView) dialog.findViewById(R.id.text);
                text.setText("Android custom dialog example!");
                ImageView image = (ImageView) dialog.findViewById(R.id.image);
                image.setImageResource(R.drawable.ic_launcher);

                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
                // if button is clicked, close the custom dialog
                dialogButton.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });

                dialog.show();
            }
        });
    }
}

This is main.xml . 这是main.xml

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

    <Button
        android:id="@+id/buttonShowCustomDialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Custom Dialog" />

</LinearLayout>

This is custom.xml : 这是custom.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="5dp" />

    <TextView
        android:id="@+id/text"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="#FFF"
        android:layout_toRightOf="@+id/image"/>/>

    <Button
        android:id="@+id/dialogButtonOK"
        android:layout_width="100px"
        android:layout_height="wrap_content"
        android:text=" Ok "
        android:layout_marginTop="5dp"
        android:layout_marginRight="5dp"
        android:layout_below="@+id/image"
        />

</RelativeLayout>

I removed one /> where you see two in custom.xml in the TextView . 我删除了一个/> ,你在TextView中的custom.xml中看到了两个。

I added View to the dialogButton.setOnClickListener as shown below: 我将View添加到了dialogButton.setOnClickListener ,如下所示:

dialogButton.setOnClickListener(new View.OnClickListener() {

I commented out the entire dialogButton.setOnClickListener . 我注释掉了整个dialogButton.setOnClickListener

I removed the line saying toRightOf...image . 我删除了说toRightOf...image的行。

I removed all objects from custom.xml except for TextView named text and removed connected code from MainActivity.java . 我从custom.xml删除了除TextView命名text以外的所有对象,并从MainActivity.java删除了连接的代码。

I debugged it and text contains the text it should but it isn't displayed. 我调试它, text包含它应该的文本,但它不显示。

All to no avail. 一切都无济于事。

Here's gradle.build for app : 这是appgradle.build

apply plugin: 'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.dslomer64.android"
        minSdkVersion 15
        targetSdkVersion 21
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.0.0'
}

I know this is going to be something trivial to an experienced Android programmer, but I just can't find it. 我知道这对经验丰富的Android程序员来说是微不足道的,但我找不到它。 And I find nothing trivial about Android GUI. 我发现Android GUI没什么微不足道的。

I am hoping that nobody will feel obligated to create a project from all these files. 我希望没有人会觉得有必要从所有这些文件创建一个项目。 I am hoping the missing connection will be obvious to a seasoned Android programmer. 我希望缺席的连接对于经验丰富的Android程序员来说是显而易见的。

In my attempt to migrate to gradle, I used this build.gradle file found at ` http://tools.android.com/tech-docs/new-build-system/migrating-from-intellij-projects ', but it didn't like this line: 在我尝试迁移到gradle时,我使用了在http://tools.android.com/tech-docs/new-build-system/migrating-from-intellij-projects '上找到的build.gradle文件,但它没有不喜欢这条线:

            classpath 'com.android.tools.build:gradle:0.5.+'


buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}
apply plugin: 'android'

dependencies {
    compile fileTree(dir: 'libs', include: '*.jar')
}

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.
        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
    }
}

The version of gradle that I have is 2.2.1, but it didn't like that either like so: 我拥有的gradle版本是2.2.1,但它不喜欢这样:

        classpath 'com.android.tools.build:gradle:2.2.1'

I see the text on your picture. 我看到你照片上的文字。 The background of the Dialog is white and you are specifying the textColor to be #FFF , that is why you cannot see it. Dialog的背景为白色,您指定textColor#FFF ,这就是您无法看到它的原因。 Change either the background of the Dialog or the color of the font. 更改Dialog的背景或字体的颜色。

The "fix" actually is Android Studio dependent: I changed the dropdown for Select theme from AppTheme to Base.Theme.appCompat . “修复”实际上取决于Android Studio:我将Select theme的下拉列表从AppThemeBase.Theme.appCompat So the code does work as posted at the link shown at beginning. 所以代码确实可以在开头显示的链接上发布。 (But this kind of stinks that it took so much effort to track down the problem!) (但这种情况很糟糕,它需要花费很多精力才能找到问题!)

HOWEVER, deleting the ill-advised text color of #FFF is a much better idea. 但是,删除#FFF的不明智的文本颜色是一个更好的主意。 (Like I said, I just copied the code.) (就像我说的,我只是复制了代码。)

The moral? 道德? If you're gonna set text color, y'oughta set the color of the dialog! 如果您要设置文本颜色,请设置对话框的颜色!

So I did. 所以我做了。 And I set the drop down theme back to AppTheme , to show that it really didn't exactly cause the problem. 我将下拉主题设置回AppTheme ,以表明它确实没有引起问题。

Here's the essence of what DID work: 这是DID工作的本质:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
    android:background="#ff4b14ff">

    <TextView
        android:id="@+id/text"
...
        android:layout_toRightOf="@+id/image"
        android:layout_toEndOf="@+id/image"
        android:textColor="#FFF"/>

I added ...toEndOf... since Android Studio advised to do so for compatibility. 我添加了...toEndOf...因为Android Studio建议这样做是为了兼容性。

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM