在显示EditText时,TextView文本未显示在ViewPager中

时间:2015-06-25 07:12:31

标签: android android-viewpager

我的布局包含TextViewEditTextListView(不太相关): business_detail_screen.xml

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

    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Details"
    android:id="@+id/business_details_caption_label"
    android:layout_gravity="center_horizontal" />

    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Address: "
        android:paddingTop="10dp"
        android:id="@+id/business_address_label"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"/>
    <EditText
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:id="@+id/business_address_edit_text"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3"/>
    </LinearLayout>
    <LinearLayout
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Available : "
        android:paddingTop="10dp"
        android:id="@+id/vacant_places_label"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"/>
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:paddingTop="10dp"
        android:id="@+id/vacant_places_data"
        android:layout_gravity="center_horizontal"
        android:layout_weight="3"/>
    </LinearLayout>

    <ListView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/waiting_list_view"
    android:layout_gravity="center_horizontal" />


</LinearLayout>

我的主要活动有一个viewpager:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.enqme.enqu.BusinessActivity"
    android:orientation="horizontal"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:showDividers="middle"
    android:focusable="false"
    android:divider="@drawable/layout_divider"
    android:background="#ffffff">

    <ListView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:id="@+id/businesses_list_view"
    android:layout_gravity="center_vertical"
    android:layout_weight="2"
    android:headerDividersEnabled="true"
    android:paddingRight="16dp"/>

    <android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/details_pager"
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_gravity="center_vertical"
    android:layout_weight="5" />
</LinearLayout>

在我的代码中,我创建了一个PagerAdapter,我将其发送到ViewPager

package com.enqme.enqu;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.enqme.enqlib.utils.BackendApi;
import com.enqme.enqlib.utils.BackendCallback;
import com.enqme.enqlib.utils.Business;
import com.enqme.enqlib.utils.LogTags;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.zip.Inflater;


public class BusinessActivity extends Activity {
    private ViewPager businessDetailsPager;
    private BusinessDetailsPagerAdapter businessDetailsAdapter;

    private class BusinessDetailsPagerAdapter extends PagerAdapter {
    private ArrayList<Business> data;
    private Context context;

    public BusinessDetailsPagerAdapter(ArrayList<Business> d, Context ctx) {
        context = ctx;
        data = d;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        int id = 0;
        switch (position) {
            case 0:
                id = R.layout.business_detail_screen_0;
                break;
            case 1:
                id = R.layout.business_detail_screen_1;
                break;
        }
        //LayoutInflater inflater = LayoutInflater.from(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(id, container, false);
        container.addView(view, 0);
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public int getCount() {
        return 2;
    }

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_business);
    currentBusiness = 0;
    businessDetailsPager = (ViewPager)findViewById(R.id.details_pager);
    businessDetailsAdapter = new BusinessDetailsPagerAdapter(businesses, this);
    businessDetailsPager.setAdapter(businessDetailsAdapter);
    }
}

ViewPager显示但问题是只显示EditTextTextView根本没有显示,没有文字。如果我将文本设置为EditText字段,则会看到它,因此不会出现问题。

请帮助:)

1 个答案:

答案 0 :(得分:0)

This happened because in the AndroidManifest.xml I had <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".SigninActivity" android:screenOrientation="landscape"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".BusinessActivity" android:screenOrientation="landscape" android:theme="@android:style/Theme.NoTitleBar"> </activity> </application> and specifically android:theme="@android:style/Theme.NoTitleBar" which caused black background, which I patched with android:background="#ffffff" in main activity. which caused the TextViews not to be visible - the background is still black and the text is black and I turn the background to white - no text (a lousy explanation)