2016-08-23 17 views
0

這是我的xml代碼,我想插入整個LinearLayout id(l1),因爲我單擊使用id(b1)定義的Mainbutton。需要注意的是linearLayout l1還包含一個textview和一個Button。 我知道如何在按鈕上點擊插入視圖,但我找不到如何在按鈕上插入整個佈局單擊。 希望不需要.java文件。請爲此定義您自己的onClick方法。如何在整個LinearLayout中的一個按鈕上的同一個活動中單擊android編程?

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:scrollbars="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.example.akash.courtcounter.MainActivity" 
    android:background="#e5e2e2"> 

    <Button 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:text="Main Button" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:textSize="15dp" 
     android:id="@+id/b1" 
     android:layout_marginBottom="10dp" /> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:background="#c5ffffff" 
     android:id="@+id/l1" 
     android:layout_height="70dp" 
     android:layout_below="@+id/b1" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true"> 

     <TextView 
      android:text="TextView Here" 
      android:textColor="#000000" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/t1" 
      android:paddingLeft="10dp" 
      android:textStyle="normal" 
      android:textSize="20dp" 
      android:layout_weight="1" /> 

     <Button 
      android:layout_width="40dp" 
      android:layout_height="wrap_content" 
      android:text="+" 
      android:id="@+id/b2" 
      android:textColor="#747070" 
      android:textSize="35dp" 
      android:background="#d3d6d8" 
      android:onClick="increase" 
      android:layout_marginRight="10dp" 
      android:layout_marginTop="10dp" /> 
    </LinearLayout> 
</RelativeLayout> 
</ScrollView> 
+1

不確定你要在這裏做什麼,但'LinearLayout'從'View'擴展,所以你可以用'View'做任何你可以用'LinearLayout'做的事情。但是,如果您只是想讓'LinearLayout'及其內容可見,那麼只需使'LinearLayout'可見即可。你已經在你的'xml'' layout'文件中找到它了。 – Abbas

+0

我只是想垂直插入一個新的佈局與格式的佈局l1彼此相鄰,不僅隱藏和取消隱藏相同的佈局 – akitsme

回答

0

作出缺席的LinearLayout從.xml文件無形

<LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="#c5ffffff" 
     android:id="@+id/l1" 
     android:visibility="gone"       //it hides the entire linear layout 
     android:orientation="vertical"> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

    //Add your views here 
     <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="1" 
        android:textColor="@color/white" /> 




    </LinearLayout> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

    //Add your views here 
     <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="2" 
        android:textColor="@color/white" /> 




    </LinearLayout> 

    <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"> 

    //Add your views here 
     <TextView 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="3" 
        android:textColor="@color/white" /> 




    </LinearLayout> 

然後在你的java文件

public class MainActivity extends AppCompatActivity { 

LinearLayout l1; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

l1 = (LinearLayout) this.findViewById(R.id.l1); 

l1.setVisibility(View.GONE); 

button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      l1.setVisibility(View.VISIBLE);   //on button click Linear Layout will be visible 


     } 
     }); 
} 

}

+0

謝謝,但我想插入多個l1佈局相互垂直相鄰 – akitsme

+0

你想插入相同的佈局多次? – Yatish

+0

是的,我想要做同樣的工作 – akitsme

0

你應該一個充氣新的佈局dyna mically並將其添加到您的父母線性佈局

LinearLayout myLayout = (LinearLayout)findViewById(R.id.linearLayout2); View newLayout = getLayoutInflater().inflate(R.layout.new_layout, null, false); myLayout.addView(newLayout);

new_layout是您的佈局要動態添加什麼。

+0

在第二行.inflate(R.layout.new_layout,null,false);只允許我插入我的xml文件名,即activity_main代替new_layout,而不是我想動態創建的佈局引用 – akitsme

+0

@akash,有兩個佈局「main_layout」,其中您的父佈局位於另一個佈局「new_layout」,其中您可以充氣並動態添加到父級佈局。 – rajesh

+0

,但它沒有給我任何建議輸入任何佈局只有建議是爲activity_main – akitsme

相關問題