简体   繁体   中英

TextView not displaying in Dialog

I am having a timer where i want to add time to the timer with from a dialog with NumberPickers. This part is working fine, but then i add TextViews on top of the pickers, just for user friendliness, but the TextViews are not displaying. What am i doing wrong? This is my XML code.

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

<RelativeLayout
    android:id="@+id/rlHour"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toLeftOf="@+id/rlMin"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvNpHours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/npHours"
        android:text="@string/hours" />

    <NumberPicker
        android:id="@+id/npHours"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlMin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvNpMinutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/npMinutes"
        android:text="@string/minutes" />

    <NumberPicker
        android:id="@+id/npMinutes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

<RelativeLayout
    android:id="@+id/rlSec"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_toRightOf="@+id/rlMin"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvNpSeconds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/npSeconds"
        android:text="@string/seconds" />

    <NumberPicker
        android:id="@+id/npSeconds"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

</RelativeLayout>

And this is how i make the dialog:

AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View dialogView = (inflater.inflate(R.layout.dialog, null));
final NumberPicker npHours = (NumberPicker) dialogView
                        .findViewById(R.id.npHours);
final NumberPicker npMinutes = (NumberPicker) dialogView
                        .findViewById(R.id.npMinutes);
final NumberPicker npSeconds = (NumberPicker) dialogView
                        .findViewById(R.id.npSeconds);
// Removed code for values of numberpickers
builder.setView(dialogView);
// Removed code for negative and positive buttons
AlertDialog dialog = builder.create();
dialog.show();

Since your NumberPickers have no layout position relationships, they are put at the top of the parent. Then, you are setting your text views above them, so they are offscreen.

Change the NumberPickers to layout_below the TextViews.

<TextView
    android:id="@+id/tvNpMinutes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/minutes" />

<NumberPicker
    android:id="@+id/npMinutes"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/tvNpMinutes" />

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