我有片段和fragment_layout,其中包含一些常见的布局,通过include标签,当我尝试以编程方式为包含的布局设置背景颜色时,首次启动片段时正确显示颜色,但在后续启动时,fragment_layout的所有子视图显示以编程方式设置颜色。我在三星-SGH-I467上使用Android 4.1.2进行了测试。
DummyFragment.java
public class DummyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dummy_details,container,false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setSiteAndPhoneText();
GridView containerAttributes = (GridView) getView().findViewById(R.id.container_attributes_grid);
List<Map.Entry<String, String>> dataForContainer = getDummyDataForContainer();
if (dataForContainer != null) {
ContainerAttributeGridAdapter adapter = new ContainerAttributeGridAdapter(this.getActivity(),
dataForContainer);
containerAttributes.setAdapter(adapter);
}
}
private void setSiteAndPhoneText() {
// Set Instructions.
View instructionHolder = getView().findViewById(R.id.includelayout3);
TextView instructionHeader = (TextView) instructionHolder.findViewById(R.id.attributerowheader);
instructionHeader.setText("ABC");
TextView instructionContent = (TextView) instructionHolder.findViewById(R.id.atributerowcontent);
instructionContent.setText("PQR");
// Set Contact.
View contactHolder = getView().findViewById(R.id.includelayout2);
TextView contactHeader = (TextView) contactHolder.findViewById(R.id.attributerowheader);
contactHeader.setText("XYZ");
TextView contactDetails = (TextView) contactHolder.findViewById(R.id.atributerowcontent);
contactDetails.setText("MNP");
//contactHolder.setBackgroundColor(Color.RED);
// Set CustomerDetails
View customerDetailsHolder = getView().findViewById(R.id.includelayout1);
TextView customerDetailsText = (TextView) customerDetailsHolder.findViewById(R.id.atributerowcontent);
TextView customerNameText = (TextView) customerDetailsHolder.findViewById(R.id.attributerowheader);
customerDetailsHolder.setBackgroundColor(Color.RED);
StringBuilder buffer = new StringBuilder(100);
customerNameText.setText("DDDDD");
customerDetailsText.setText("ddddddmmmmm");
}
private List<Map.Entry<String, String>> getDummyDataForContainer() {
List<Map.Entry<String, String>> containerAttributesList = new ArrayList<Map.Entry<String, String>>();
Map.Entry<String, String> size = new ContainerAttributes<String, String>("Size", "10");
Map.Entry<String, String> type = new ContainerAttributes<String, String>("Type", "10");
Map.Entry<String, String> truck = new ContainerAttributes<String, String>("Truck", "10");
Map.Entry<String, String> material = new ContainerAttributes<String, String>("Material", "10");
Map.Entry<String, String> wheels = new ContainerAttributes<String, String>("Wheels", "10");
Map.Entry<String, String> locks = new ContainerAttributes<String, String>("Locks", "10");
Map.Entry<String, String> lids = new ContainerAttributes<String, String>("Lids", "10");
Map.Entry<String, String> others = new ContainerAttributes<String, String>("Others", "10");
containerAttributesList.add(size);
containerAttributesList.add(type);
containerAttributesList.add(truck);
containerAttributesList.add(material);
containerAttributesList.add(wheels);
containerAttributesList.add(locks);
containerAttributesList.add(lids);
containerAttributesList.add(others);
return containerAttributesList;
}
}
dummy_details.xml
包含没有任何背景的相对布局。
<RelativeLayout 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:padding="1dp" >
<include
android:id="@+id/includelayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
layout="@layout/include_layout_details" />
<GridView
android:id="@+id/container_attributes_grid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/includelayout1"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:background="#E2E2E2"
android:gravity="center_horizontal"
android:numColumns="4"
android:stretchMode="columnWidth" >
</GridView>
<include
android:id="@+id/includelayout2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:paddingLeft="3dp"
android:layout_marginRight="3dp"
android:layout_above="@id/container_attributes_grid"
layout="@layout/include_layout_details" />
<include
android:id="@+id/includelayout3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_below="@id/container_attributes_grid"
android:layout_gravity="center"
layout="@layout/include_layout_details" />
</RelativeLayout>
include_layout_details.xml
包含linearlayout,背景为E2E2E2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#E2E2E2"
android:orientation="vertical"
android:padding="3dp"
>
<TextView
android:id="@+id/attributerowheader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
/>
<TextView
android:id="@+id/atributerowcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:textStyle="bold"
/>
</LinearLayout>
注意:我没有包含GridAdapter文件,但GridAdapter正在使用下面的文件进行行显示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
>
<TextView
android:id="@+id/detailsrowheader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="@+id/detailsrowcontent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:singleLine="true"
android:layout_marginBottom="10dp"/>
</LinearLayout>
首次启动后,它会显示所有<include>
视图的正确颜色,但在下次启动片段后,它会在所有<include>
视图和gridview中显示红色。
代码在Nexus 5 6.0中运行良好,但在使用4.1.2 Samsung Tab进行测试时,它无效。
答案 0 :(得分:0)
您可以将包含的布局背景及其子视图背景设置为android:background="#00000000"
,这样可以使其透明
。
然后在你的片段中获得视图的引用;您只需编写
即可在运行时指定背景颜色view1.setBackgroundColor(Color.BLUE); //your color choice
view2.setBackgroundColor(Color.GREEN);
希望这有帮助。