0

enter image description here如何获取对话框片段或窗口的背景颜色?

红色文字出来了一点点。它是这样说的:

需要获取对话框的背景颜色,以便我可以将“more info”textview的背景设置为相同的颜色。需要以编程方式执行此操作,因为对话框的颜色因API级别而异。

Java代码:

public class CityDetailDialog extends DialogFragment { 

    private SharedPreferences sharedPreferences; 
    private City mCity; 
    private LinearLayout mCityContainerLayout; 
    private ImageView mImageFlag; 
    private TextView mTextCity; 
    private ImageView mImageCity; 
    private ImageButton mButtonGoogleMaps; 
    private ImageButton mButtonWikipedia; 
    private TextView mMoreInfoTextView; 

    public CityDetailDialog() { 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, 
          @Nullable Bundle savedInstanceState) { 
     sharedPreferences = getActivity().getSharedPreferences("settings", Context.MODE_PRIVATE); 
     return inflater.inflate(R.layout.dialog_city_detail, container, false); 
    } 

    @Override 
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { 
     super.onViewCreated(view, savedInstanceState); 
     mCity = (City) getArguments().getSerializable("city"); 
     mCityContainerLayout = (LinearLayout) view.findViewById(R.id.container_city_dialog); 
     mImageFlag = (ImageView) view.findViewById(R.id.flag_image); 
     mTextCity = (TextView) view.findViewById(R.id.city_name); 
     mImageCity = (ImageView) view.findViewById(R.id.city_image); 
     mButtonGoogleMaps = (ImageButton) view.findViewById(R.id.button_google_maps); 
     mButtonWikipedia = (ImageButton) view.findViewById(R.id.button_wikipedia); 
     mMoreInfoTextView = (TextView) view.findViewById(R.id.more_info_label); 

     mImageFlag.setImageResource(ResourceByNameRetriever.getDrawableResourceByName(mCity. 
       getCountryName(), getActivity())); 
     mTextCity.setText(ResourceByNameRetriever.getStringResourceByName(mCity.getCityName(), 
       getActivity())); 
     Picasso.with(getActivity()).load(mCity.getImageUrl()).into(mImageCity); 

     mButtonGoogleMaps.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent mapsIntent = new Intent(Intent.ACTION_VIEW); 
       mapsIntent.setData(Uri.parse("geo:" + mCity.getCoordinates())); 
       Intent chooser = Intent.createChooser(mapsIntent, getString(R.string.launch_maps)); 
       startActivity(chooser); 
      } 
     }); 

     mButtonWikipedia.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent wikiIntent = new Intent(Intent.ACTION_VIEW); 
       wikiIntent.setData(Uri.parse(mCity.getWikiUrl())); 
       Intent chooser = Intent.createChooser(wikiIntent, getString(R.string.launch_browser)); 
       startActivity(chooser); 
      } 
     }); 
    } 

}

+0

请分享您编写的用于实现对话框的java代码。 –

+0

@KavachChandra added – Nico

+0

所以我试着用各种方法运行应用程序,例如mMoreInfoTextView.bringToFront(),并将背景颜色设置为textView,但无法达到您在右侧图片中显示的效果。 TexView总是以它下面的元素为背景,即当我穿过它的线时,即使当我将视图放到前面时,该线仍然可见。我能想到的唯一解决方案是修改相关的xml布局。如果您认为它也可以提供解决方案,请分享该代码。 –

回答

1

基于以下需要更改进行以达到预期的效果在XML文件中的代码在GitHub上:

<LinearLayout 
     android:id="@+id/container_buttons" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:baselineAligned="false" 
     android:orientation="horizontal" 
     android:layout_below="@+id/relativeLayout"> 

. 
. 
. 
</LinearLayout> 
<RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:id="@+id/relativeLayout"> 

     <View 
      android:id="@+id/horizontal_divider1" 
      android:layout_width="wrap_content" 
      android:layout_height="2dp" 
      android:layout_centerInParent="true" 
      android:background="@color/gray" 
      android:layout_toLeftOf="@+id/more_info_label" 
      android:layout_toStartOf="@+id/more_info_label"/> 


     <TextView 
      android:id="@+id/more_info_label" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:padding="5dp" 
      android:text="@string/more_info" 
      android:textSize="18dp" /> 

     <View 
      android:id="@+id/horizontal_divider2" 
      android:layout_width="wrap_content" 
      android:layout_height="2dp" 
      android:layout_centerInParent="true" 
      android:background="@color/gray" 
      android:layout_toRightOf="@+id/more_info_label" 
      android:layout_toEndOf="@+id/more_info_label"/> 

    </RelativeLayout>