2016-07-21 4 views
0

私はメインレイアウトを持っています。これはCollapsingToolbarLayoutであり、その中にカスタムレイアウト/ビューがあります。私はこのカスタムレイアウトを 'include'タグを使ってCollapsingToolbarに追加しました。これはうまく表示されますが、カスタムレイアウトをアクティビティから膨らませていません。これは:'include'タグを使用して親レイアウトによって参照されるインフレータブルレイアウト

RelativeLayout r = (RelativeLayout) findViewById(R.id.create_quote_toolbar_layout); 
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    View v = inflater.inflate(R.layout.create_quote_toolbar, r, false); 
    TextView T = (TextView)v.findViewById(R.id.lineTotal); 
    T.setText("sdrsfLineTotal"); 

は動作していないようです。

main_activity_layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context=".CreateQuoteActivity" > 

    <android.support.design.widget.AppBarLayout 
     android:id="@+id/app_bar_layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:fitsSystemWindows="true" 
     android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" > 

     <android.support.design.widget.CollapsingToolbarLayout 
      android:id="@+id/collapsing_toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      app:layout_scrollFlags="scroll|exitUntilCollapsed" 
      android:fitsSystemWindows="true" 
      app:contentScrim="?attr/colorPrimary" 
      app:expandedTitleMarginEnd="100dp" 
      app:expandedTitleMarginStart="48dp" > 

      <include layout="@layout/create_quote_toolbar"/> 

      <android.support.v7.widget.Toolbar 
       android:id="@+id/toolbar" 
       android:layout_width="match_parent" 
       android:layout_height="?attr/actionBarSize" 
       app:layout_collapseMode="pin" 
       app:popupTheme="@style/ThemeOverlay.AppCompat.Light" /> 

     </android.support.design.widget.CollapsingToolbarLayout> 
    </android.support.design.widget.AppBarLayout> 

    <android.support.v4.widget.NestedScrollView 
     android:id="@+id/scroll" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     app:layout_behavior="@string/appbar_scrolling_view_behavior" 
     android:clipToPadding="false" > 

     <ListView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@android:id/list"/> 

    </android.support.v4.widget.NestedScrollView> 

</android.support.design.widget.CoordinatorLayout> 

custom_toolbar_layout.xml:あなたはsetContentViewと活動のルートビューを設定している場合は、膨らませる理由はない

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

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/deliverToAddress" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="16dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/phoneNumber" 
     android:layout_below="@+id/deliverToAddress" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="16dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/name" 
     android:layout_below="@+id/phoneNumber" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="16dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/date" 
     android:layout_below="@+id/name" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:textSize="16dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Valid Until:" 
     android:id="@+id/textView5" 
     android:layout_below="@+id/date" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:layout_marginTop="13dp" 
     android:textSize="16dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/expirationDate" 
     android:layout_below="@+id/date" 
     android:layout_toRightOf="@+id/textView5" 
     android:layout_toEndOf="@+id/textView5" 
     android:hint="dd/mm/yyyy" 
     android:textSize="16dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/total" 
     android:layout_below="@+id/expirationDate" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentEnd="true" 
     android:textSize="16dp"/> 

    <EditText 
     android:layout_width="200dp" 
     android:layout_height="wrap_content" 
     android:id="@+id/itemDescription" 
     android:layout_centerVertical="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" 
     android:hint="Item Description" 
     android:textSize="16dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/quantity" 
     android:layout_alignTop="@+id/itemDescription" 
     android:layout_toRightOf="@+id/itemDescription" 
     android:layout_toEndOf="@+id/itemDescription" 
     android:hint="Quantity" 
     android:textSize="16dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/unitPrice" 
     android:layout_below="@+id/quantity" 
     android:layout_toRightOf="@+id/itemDescription" 
     android:layout_toEndOf="@+id/itemDescription" 
     android:hint="Unit Price" 
     android:textSize="16dp"/> 

    <EditText 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/discount" 
     android:layout_below="@+id/unitPrice" 
     android:layout_alignLeft="@+id/unitPrice" 
     android:layout_alignStart="@+id/unitPrice" 
     android:hint="Discount" 
     android:textSize="16dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Text" 
     android:id="@+id/lineTotal" 
     android:textSize="16dp" 
     android:layout_below="@+id/discount" 
     android:layout_alignRight="@+id/discount" 
     android:layout_alignEnd="@+id/discount"/> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Add Item" 
     android:id="@+id/addItemButton" 
     android:layout_alignParentBottom="true" 
     android:layout_toLeftOf="@+id/expirationDate" 
     android:layout_toStartOf="@+id/expirationDate"/> 

    <Button 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Send Quote" 
     android:id="@+id/respondButton" 
     android:layout_alignParentBottom="true" 
     android:layout_alignRight="@+id/total" 
     android:layout_alignEnd="@+id/total"/> 

</RelativeLayout> 
+0

なぜあなたはそれを膨らませたいですか?あなたの目標は何ですか? – Divers

+0

Ivにボタンや他のウィジェットがあります。リスナーを設定する必要があります。基本的には機能します。 – BiGGZ

+0

あなたは膨張していますが、そのビューをアクティビティのビューにサブビューとして追加していますか? –

答えて

0

景色。 xmlでタグを使用すると、アクティビティでタグを設定すると自動的にビューが膨張します。

ビューを検索する場合は、通常、他のIDのIDとしてfindViewByIdを実行します。

たとえば、あなたはあなたのonCreateメソッドでないでしょう:あなたは、単に

findViewById(create_quote_toolbar_layout)

を行うことができます

setContentView(R.layout.main_activity_layout)

この後あなたが含まれているビューでRelativeLayoutが見つかります。

+0

"setContentViewを使用してアクティビティのルートビューを設定した場合、ビューを膨張させる理由はありません。xmlでタグを使用すると、アクティビティに設定すると自動的にビューが膨張します。 "だからシンプルな...そのトリックをした。 – BiGGZ

関連する問題