2014-10-31 50 views
1

我想要有两个或更多的片段将被类似地处理。我可以在每个片段中重复使用ID吗?两个Android碎片可以使用相同的ID吗?

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/fragment_container" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    <Fragment 
    android:id="@+id/fragment_1" 
    class="com.example.DoesSomethingToTitle" 

    <TextView android:id="@+id/title" /> 
    </Fragment> 


    <Fragment 
    android:id="@+id/fragment_2" 
    class="com.example.DoesSomethingToTitle" 

    <TextView android:id="@id/title" /> <!-- same id --> 
    </Fragment> 

</FrameLayout> 

而且使用这样的:

public class DoesSomethingToTitle { 
    private String titletext; 

    public DoesSomethingToTitle(String titletext) { 
     this.titletext = titletext; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View v = inflater.inflate(R.layout.feed, container, false); 
     // find the title TextView and mutate it 
     ((TextView) v.findViewById(R.id.title)).setText(titletext); 
     return v; 
    } 
} 

(我知道对于这样简单的东西,我可以只指定XML中的标题字符串;我的实际使用情况比较复杂)

+0

我不确定问题是否有意义。我可以问你为什么要重用ID吗? – 2014-10-31 20:49:38

+0

因为我想要一个类findViewById,并且我想传递相同的ID。 – tpdi 2014-11-01 02:15:00

回答

1

我可以在每个片段中重新使用ID吗?

是的,你可以。

相关问题