[英]Why won't `text` show when customdialog is opened?
我觉得自己像个白痴。 我从这里复制了这个项目。 该zip文件不会导入Android Studio 1.0.2。 建议是将其迁移到Gradle,但我不知道该怎么做。 (我后来找到了这样做的链接。我无法实现它。它处于这个混乱的底部。)
所以我创建了一个新项目并剪切并粘贴了3 xml
和1个java
文件。 我终于得到了汇编。
据说下面的对话框会显示出来,但是当我运行它时, 它不显示文本text
,这是“ Android自定义对话框示例 ”。 它只显示图标; 在custom.xml
指定的文本根本没有。 我花了好几个小时(我很清楚没有掌握Android java或xml或连接 - 我正在研究它 - 但我看到我期望在java和xml中看到TextView
命名text
)试图解决这个问题。 我现在希望你们都能帮助我。
我尝试过的(徒劳)列在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>
我删除了一个/>
,你在TextView
中的custom.xml
中看到了两个。
我将View
添加到了dialogButton.setOnClickListener
,如下所示:
dialogButton.setOnClickListener(new View.OnClickListener() {
我注释掉了整个dialogButton.setOnClickListener
。
我删除了说toRightOf...image
的行。
我从custom.xml
删除了除TextView
命名text
以外的所有对象,并从MainActivity.java
删除了连接的代码。
我调试它, text
包含它应该的文本,但它不显示。
一切都无济于事。
这是app
的gradle.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'
}
我知道这对经验丰富的Android程序员来说是微不足道的,但我找不到它。 我发现Android GUI没什么微不足道的。
我希望没有人会觉得有必要从所有这些文件创建一个项目。 我希望缺席的连接对于经验丰富的Android程序员来说是显而易见的。
在我尝试迁移到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')
}
}
我拥有的gradle版本是2.2.1,但它不喜欢这样:
classpath 'com.android.tools.build:gradle:2.2.1'
我看到你照片上的文字。 Dialog
的背景为白色,您指定textColor
为#FFF
,这就是您无法看到它的原因。 更改Dialog
的背景或字体的颜色。
“修复”实际上取决于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建议这样做是为了兼容性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.