[英]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.