如何在包含的布局中以编程方式添加TextView?

时间:2017-07-01 10:17:54

标签: android android-layout xamarin xamarin.android

我有System.out.println("Auftrag: " + super.getSollAuftrag1()); System.out.println("Quote: " + super.getSollQuote1()); System.out.println("Teil: " + super.getSollTeil1()); System.out.println("Stueck: " + super.getSollStueck1()); ,其他布局public class ReadText { public static String sollAuftrag1; public static String sollQuote1; public static String sollTeil1; public static String sollStueck1; public static String parts; public static String getParts() { return parts; } public static void setParts(String parts) { ReadText.parts = parts; } protected String getSollAuftrag1() { return sollAuftrag1; } protected void setSollAuftrag1(String sollAuftrag) { ReadText.sollAuftrag1 = sollAuftrag; } public String getSollQuote1() { return sollQuote1; } public void setSollQuote1(String sollQuote) { ReadText.sollQuote1 = sollQuote; } public String getSollTeil1() { return sollTeil1; } public void setSollTeil1(String sollTeil) { ReadText.sollTeil1 = sollTeil; } public String getSollStueck1() { return sollStueck1; } public void setSollStueck1(String sollStueck) { ReadText.sollStueck1 = sollStueck; } public static void main(String[] args) { // TODO Auto-generated method stub BufferedReader br = null; String file_at_path_to_desktop = System.getProperty("user.home") + "\\" + "Desktop" + "\\" + "SollAuftrag.txt"; try { br = new BufferedReader(new FileReader(new File(file_at_path_to_desktop))); String line = null; while ((line = br.readLine()) != null) { new ArrayList<String>(Arrays.asList(line.split(";"))); String[] parts = line.split(";"); sollAuftrag1 = (parts[0]); sollQuote1 = (parts[1]); sollTeil1 = (parts[0]); sollStueck1 = (parts[1]); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } } System.out.println("Auftrag: " + sollAuftrag1); System.out.println("Unterauftrag: " + sollQuote1); System.out.println("Teil: " + sollTeil1); System.out.println("Stueck: " + sollStueck1); } } BaseLayout。换句话说,其他布局继承/包含它。 BaseLayoutParentLayout(所有布局共享)但每个BaseLayout都有不同的内容。

我的BaseLayout看起来像这样:

DrawerLayout

并包含在Activity中,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="right"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />
  <android.support.v4.widget.DrawerLayout
    android:id="@+id/myDrawer"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--Main Content-->
    <LinearLayout
      android:id="@+id/rootLinearLayout"
      android:layout_height="match_parent"
      android:layout_width="match_parent"
      android:orientation="vertical">
      <!--Content-->

    <!--must add Other Layout elements there 
     must add Other Layout elements there 
     must add Other Layout elements there 
     must add Other Layout elements there 
     must add Other Layout elements there !-->

    </LinearLayout>
    <!-- Left Navigation Derawer -->
    <LinearLayout
      android:id="@+id/llLeftDrawer"
      android:orientation="vertical"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:background="?attr/colorAccent"
      android:padding="15dp"
      android:layout_gravity="right">
      <!-- Left Navigation Derawer Content-->
      <TextView
        android:text="Categories"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/textView1"
        android:gravity="center"
        android:textColor="#FFFFFF"
        fontPath="fonts/MobileFont.ttf" />
      <ListView
        android:id="@+id/lvLeftMenu"
        android:layout_height="match_parent"
        android:layout_width="match_parent" />
    </LinearLayout>
  </android.support.v4.widget.DrawerLayout>
</LinearLayout>

如何将Postlayout元素添加到<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <include layout="@layout/BaseLayout" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> PostLayout ID BaseLayout

LinearLayout元素就是这样:

rootLinearLayout

3 个答案:

答案 0 :(得分:0)

首先,为id中包含的Baselayout提供Postlayout属性。

然后,您可以通过从包含View的活动中调用以下内容,从包含的视图中获取Postlayout对象: -

View includedView = (View)findViewById(R.id.idThatYouGaveToIncludedLayout);

然后,您可以从Baselayout对象访问所有includedView个视图,并执行所需的操作。

例如,

LinearLayout rootLinearLayout = (LinearLayout)includedView.findViewById(R.id.rootLinearLayout);

与上述代码类似,您可以获取Baselayout的其他视图的对象。

答案 1 :(得分:0)

Bruno Bieri得到solution是java,我转换为xamarin:

LayoutInflater inflater = Application.Context.GetSystemService(Context.LayoutInflaterService) as LayoutInflater;
View v = inflater.Inflate(Resource.Layout.your_layout, null);

// fill in any details dynamically here
TextView textView = v.FindViewById<TextView>(Resource.Id.a_text_view);
textView.Text="your text";

// insert into main view
ViewGroup insertPoint = FindViewById<ViewGroup>(Resource.id.insert_point);
insertPoint.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));

和Rohan Stark解决方案适合访问包含布局的元素

tanQ Bruno Bieri和Rohan Stark,

答案 2 :(得分:0)

当您将视图添加为包含时,可以使用所有视图,如自己的主要布局视图。找到布局作为视图并添加视图。如下所示:

LinearLayout linearLayout1 = (LinearLayout)findViewById(R.id.linearLayout1);

 linearLayout1.addView(new TextView(context));