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

时间:2015-04-21 18:04:43

标签: java android xml

我觉得自己像个白痴。我从here复制了这个项目。该zip文件无法导入Android Studio 1.0.2。建议将其迁移到Gradle,但我不知道该怎么做。 (我后来找到了这样做的链接。我无法实现它。它处于这个混乱的底部。)

所以我创建了一个新项目并剪切并粘贴了3 xml和1 java文件。我终于得到了汇编。

据说下面的对话框会显示,但是当我运行它时,它没有显示 text的文字,这是" Android自定义对话框示例"。它只显示图标; custom.xml中指定的根本没有任何文本。我花了好几个小时(我很清楚没有掌握Android java或xml或连接 - 我正在研究它 - 但我看到了我希望在java和xml中看到的{{ 1}}命名TextView)试图解决这个问题。我现在希望你们都能帮助我。

我尝试过的(徒劳)列在text文件下面。

enter image description here

编辑 - 这里'我看到了什么:

enter image description here 这是custom.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>

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(); } }); } }

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>

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> ,您在/>的{​​{1}}中看到了两个{。}}。

我将custom.xml添加到TextView,如下所示:

View

我评论了整个dialogButton.setOnClickListener

我删除了dialogButton.setOnClickListener(new View.OnClickListener() {

这一行

我删除了dialogButton.setOnClickListener中的所有对象,但名为toRightOf...image的{​​{1}}除外,并从custom.xml删除了已连接的代码。

我对其进行了调试,TextView包含了应该显示的文字,但它没有显示。

一切都无济于事。

这里text MainActivity.java

text

我知道这对经验丰富的Android程序员来说是微不足道的,但我无法找到它。我发现Android GUI没有什么微不足道的。

我希望没有人会觉得有必要从所有这些文件中创建一个项目。我希望缺少的连接对于经验丰富的Android程序员来说是显而易见的。

在我尝试迁移到gradle时,我使用了http://tools.android.com/tech-docs/new-build-system/migrating-from-intellij-projects&#39;中找到的gradle.build文件,但它并不喜欢这一行:

app

我拥有的gradle版本是2.2.1,但它不喜欢这样:

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'
}

2 个答案:

答案 0 :(得分:3)

我看到你照片上的文字。 Dialog的背景为白色,您指定textColor#FFF,这就是您无法看到它的原因。更改Dialog的背景或字体的颜色。

答案 1 :(得分:0)

&#34;修复&#34;实际上取决于Android Studio:我将Select theme的下拉列表从AppTheme更改为Base.Theme.appCompat。所以代码确实可以在开头显示的链接上发布。 (但这种情况很糟糕,它需要花费很多精力来追查问题!)

但是,删除#FFF的不明智的文字颜色是一个更好的主意。 (就像我说的,我只是复制了代码。)

道德?如果你要设置文字颜色,你可以设置对话框的颜色!

所以我做到了。我将下拉主题设置回AppTheme,以表明它确实没有引起问题。

这里是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"/>

我添加了...toEndOf...,因为Android Studio建议这样做是为了兼容性。

enter image description here