简体   繁体   中英

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. Advice was to migrate it to Gradle, but I don't know how to do so. (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. 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 ". It just shows the icon; no text at all to its right as specified in 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. I am now hoping you all will give me a hand.

What I have tried (in vain) is listed below the custom.xml file.

在此输入图像描述

EDIT--HERE'S WHAT I DO SEE:

在此输入图像描述 This is 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 :

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 .

<?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 :

<?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 .

I added View to the dialogButton.setOnClickListener as shown below:

dialogButton.setOnClickListener(new View.OnClickListener() {

I commented out the entire dialogButton.setOnClickListener .

I removed the line saying toRightOf...image .

I removed all objects from custom.xml except for TextView named text and removed connected code from MainActivity.java .

I debugged it and text contains the text it should but it isn't displayed.

All to no avail.

Here's gradle.build for app :

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. And I find nothing trivial about 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.

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:

            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:

        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. Change either the background of the Dialog or the color of the font.

The "fix" actually is Android Studio dependent: I changed the dropdown for Select theme from AppTheme to Base.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. (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.

Here's the essence of what DID work:

<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.

在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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