如果两个布局具有相同的视图ID,如何获取特定视图ID?

时间:2011-08-05 10:07:08

标签: android android-layout

我在同一项目中有两个布局,内容TextView具有相同的ID。我想访问特定布局的文本视图ID。怎么弄明白?

EX。

如何访问Layout1.xml的文本视图?

layout1 XML文件:

   <RelativeLayout 
    android:id="@+id/TopPanel"
    android:layout_width="fill_parent" 
    android:background="@drawable/header" 
    android:layout_height="45dip"
    android:gravity="top"
    android:orientation="horizontal">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="4dip"
        android:id="@+id/title1" 
        android:background="@color/transparent"
        android:textColor="@color/white" 
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:singleLine="true" 
        android:text="@string/AllMessages" 
        android:textStyle="bold"/>
</RelativeLayout>

layout2 XML文件:

    <FrameLayout 
    android:id="@+id/flayout"
    android:layout_width="fill_parent" 
    android:background="@drawable/header" 
    android:layout_height="45dip"
    android:gravity="top"
    android:orientation="horizontal">

    <TextView 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:paddingLeft="4dip"
        android:id="@+id/title1" 
        android:background="@color/transparent"
        android:textColor="@color/white" 
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:singleLine="true" 
        android:text="@string/AllMessages" 
        android:textStyle="bold"/>
     </FrameLayout>

代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.layout1);
  View vw =findViewById(R.layout.layout1);
  TextView tvBannerText =(TextView) vw.findViewById(R.id.title1);
      tvBannerText.setTextColor(bannerTextColor);
      tvBannerText.invalidate();

1 个答案:

答案 0 :(得分:0)

然后你可以这样做..

TextView t1 = layout1.findViewById(R.id.yourTextView);

Layout l2 = layout1.findViewById(R.Layout.layout2);

TextView t1 = l2.findViewById(R.id.yourTextView);