2017-04-17 62 views
0

我有一個Listview包含文本和綠色正方形的活動。我終於改變了主意,並修改了我的代碼以使用片段而不是活動。但隨後我的文字顏色發生了變化,我再也看不到綠色方塊了。我沒有改變我的列表代碼或文本的顏色,所以我不明白髮生了什麼。從活動切換到片段後,用戶界面外觀發生變化

謝謝你的幫助。

enter image description here

MainActivity僅作爲一個片段容器:

public class MainActivity extends AppCompatActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     FragmentManager fm = getSupportFragmentManager(); 

     Fragment fragment = fm.findFragmentById(R.id.ll_container); 
     if (fragment == null) { 
      fragment = new PollutionLevelsFragment(); 
      fm.beginTransaction() 
        .add(R.id.ll_container, fragment) 
        .commit(); 
     } 
    } 
} 

MainActivity佈局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/ll_container" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

片段類

public class PollutionLevelsFragment extends Fragment implements PollutionLevelsFragmentMVP.View { 
    private PollutionLevelAdapter plAdapter; 

    @BindString(R.string.city) 
    String city; 

    @BindString(R.string.aqicn_token) 
    String authToken; 


    @BindView(R.id.lv_pollution_levels) 
    ListView lvPollutionLevels; 

    @Inject 
    PollutionLevelsFragmentMVP.Presenter presenter; 

    private ViewModel pollutionData; 
    private ArrayList<String> testbidon; 

    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
     View view = inflater.inflate(R.layout.fragment_pollution_levels, container, false); 

     ButterKnife.bind(this,view); 

     // Construct the data source 
     ArrayList<PollutionLevel> arrayOfPollutionLevel = new ArrayList<>(); 
     // Create the adapter to convert the array to views 
     plAdapter = new PollutionLevelAdapter(getActivity().getApplicationContext(), arrayOfPollutionLevel); 

     lvPollutionLevels.setAdapter(plAdapter); 
     return view; 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 

     presenter.setView(this); 
     presenter.loadData(city, authToken); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     presenter.rxUnsubscribe(); 
    } 

    @Override 
    public void onActivityCreated(@Nullable Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 

     ((App) getActivity().getApplication()).getComponent().inject(this); 
    } 

    @Override 
    public void updateData(ViewModel viewModel) { 
     this.pollutionData = viewModel; 

     ArrayList<PollutionLevel> pollutionLevels = viewModel.getAllPolluants(); 

     for(PollutionLevel pl : pollutionLevels) { 
      plAdapter.add(pl); 
     } 
    } 
} 

片段佈局

<FrameLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <ListView android:id="@+id/lv_pollution_levels" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 
</FrameLayout> 

爲ListView

public class PollutionLevelAdapter extends ArrayAdapter<PollutionLevel> { 

@BindView(R.id.tv_name) 
TextView tvName; 

@BindView(R.id.tv_value) 
TextView tvValue; 

public PollutionLevelAdapter(Context context, ArrayList<PollutionLevel> pollutionLevels) { 
    super(context,0,pollutionLevels); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_pollution, parent, false); 
    } 

    View view = convertView; 

    ButterKnife.bind(this,view); 
    PollutionLevel pollutionLevel = getItem(position); 
    tvName.setText(pollutionLevel.getName()); 
    tvValue.setText(pollutionLevel.getValue()); 


    return view; 
} 
} 

我的列表項一個ArrayAdapter

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/iv_warning" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    app:srcCompat="@drawable/rectangle"/> 

<TextView 
    android:id="@+id/tv_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView"/> 

<TextView 
    android:id="@+id/tv_value" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView"/> 

這是佈局的代碼,呈現Activity類正確的列表視圖(所以之前我從一個活動到一個片段)。我沒有把ListView的適配器的代碼在這裏,因爲它並沒有改變:

活動

public class PollutionLevelsActivity extends AppCompatActivity implements PollutionLevelsActivityMVP.View { 
    private PollutionLevelAdapter plAdapter; 

    @BindString(R.string.city) 
    String city; 

    @BindString(R.string.aqicn_token) 
    String authToken; 


    @BindView(R.id.lv_pollution_levels) 
    ListView lvPollutionLevels; 

    @Inject 
    PollutionLevelsActivityMVP.Presenter presenter; 

    private ViewModel pollutionData; 
    private ArrayList<String> testbidon; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ((App) getApplication()).getComponent().inject(this); 
     ButterKnife.bind(this); 

    // Construct the data source 
    ArrayList<PollutionLevel> arrayOfPollutionLevel = new ArrayList<>(); 
    // Create the adapter to convert the array to views 
    plAdapter = new PollutionLevelAdapter(this, arrayOfPollutionLevel); 

    lvPollutionLevels.setAdapter(plAdapter); 

    } 

    @Override 
    protected void onStart() { 
     super.onStart(); 

     presenter.setView(this); 
     presenter.loadData(city, authToken); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     presenter.rxUnsubscribe(); 
    } 

    @Override 
    public void updateData(ViewModel viewModel) { 
     this.pollutionData = viewModel; 

     ArrayList<PollutionLevel> pollutionLevels = viewModel.getAllPolluants(); 

     for(PollutionLevel pl : pollutionLevels) { 
      plAdapter.add(pl); 
     } 
    } 
} 

佈局

<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.livos.citypollution.pollutionlevels.PollutionLevelsActivity"> 

    <ListView 
     android:id="@+id/lv_pollution_levels" 
     android:layout_width="368dp" 
     android:layout_height="495dp" 
     tools:layout_editor_absoluteX="8dp" 
     tools:layout_editor_absoluteY="8dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent"/> 

</android.support.constraint.ConstraintLayout> 
+0

我剛剛解決了通過改變這個'a pp:srcCompat =「@ drawable/rectangle」'這個android:src =「@ drawable/rectangle」'。我仍然有問題的文字顏色。 – Laurent

回答

0

要更改文本顏色添加此

android:textColor =「您的顏色代碼」

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      xmlns:app="http://schemas.android.com/apk/res-auto" 
      android:orientation="horizontal" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

<ImageView 
    android:id="@+id/iv_warning" 
    android:layout_width="20dp" 
    android:layout_height="20dp" 
    **app:src="@drawable/rectangle"**/> 

<TextView 
    android:id="@+id/tv_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView" 
    android:textColor="Your Color Code"/> 

<TextView 
    android:id="@+id/tv_value" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:gravity="center_vertical" 
    android:text="TextView" 
    android:textColor="Your Color Code"/> 
+0

是的,但通常情況下,如果您不提供特定顏色,則默認顏色爲黑色。但我認爲我現在明白了,我認爲這與主題不適用於片段但適用於活動有關。或者應用不同的主題... – Laurent

+0

是的,但是你知道是否有方法將活動中的主題用於我的片段? (如果它確實是一個主題問題) – Laurent

+1

我認爲在Manifest中定義的主題只適用於活動而非片段。我想我在這裏找到了一些東西http://stackoverflow.com/questions/9469174/set-theme-for-a-fragment – Laurent

0

因爲我越來越。你給適配器,如:

public class PollutionLevelAdapter extends ArrayAdapter<PollutionLevel> { 

@BindView(R.id.tv_name) 
TextView tvName; 

@BindView(R.id.tv_value) 
TextView tvValue; 

public PollutionLevelAdapter(Context context, ArrayList<PollutionLevel> pollutionLevels) { 
    super(context,0,pollutionLevels); 
} 


@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    if(convertView == null) { 
     convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_pollution, parent, false); 
    } 

    View view = convertView; 

    ButterKnife.bind(this,view); 
    PollutionLevel pollutionLevel = getItem(position); 
    tvName.setText(pollutionLevel.getName()); 
    tvValue.setText(pollutionLevel.getValue()); 


    return view; 
} 
} 

你結合你的兩個TextView不是你ImageView因爲你給你的list_item_pollution,這是有ImageView讓你的ImageView綁定,如:

@BindView(R.id.iv_warning) 
ImageView iv_warning; 

,並在getView方法集是這樣的:

iv_warning.setBackgroundColor(Color.parseColor("your color code")); 
+0

因爲我知道ButterKnife @BindView用於避免findViewById()樣板,如果我不需要更改我的方形顏色我不需要爲我的ImageView使用@BindView,因爲正方形和它的顏色是通過使用我的列表項目佈局的PollutionLevelAdapter定義的。此列表項目佈局包含我的imageview,其中定義了可繪製的src。顏色在這個可繪製的src中定義。順便說一下,我的解決方案只是使用'android:src =「@ drawable/rectangle」'。 – Laurent

+0

對於文本顏色,通常有一個默認的主題顏色,如果沒有指定任何顏色,則爲黑色。經過一些更多的研究後,我認爲在活動中有一個默認主題不適用於片段,但我不知道爲什麼。 – Laurent