Как получить конкретный идентификатор вида, если два макета имеют одинаковый идентификатор вида? - PullRequest
2 голосов
/ 05 августа 2011

У меня есть два макета в одном проекте, содержимое которых TextView имеет одинаковый идентификатор.Я хочу получить доступ к идентификатору текстового представления определенного макета.как получить?

EX.

как получить доступ к TextView из 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 голосов
/ 05 августа 2011

тогда вы можете сделать это ..

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

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

TextView t1 = l2.findViewById(R.id.yourTextView);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...