[英]Two LinearLayouts at same position
我想在寬度與父代相同的位置添加兩個線性布局。 當我按下Button1時,應該出現linearLayout1,而在Button2上,應該出現LinearLayout2。 如何在android中做到這一點?
嘗試以下操作:首先創建your_xml_file.xml
<?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="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/firstLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<LinearLayout
android:id="@+id/secondLinear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button1"
android:focusable="false"
android:layout_centerInParent="true"
android:background="@drawable/ic_launcher"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button2"
android:focusable="false"
android:background="@drawable/ic_launcher"
android:layout_below="@id/button1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
和Java clas ...
public class YourActivity extends Activity {
private Button button1,button2;
private LinearLayout linearLayout1,linearLayout2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourXml);
button1 = (Button).findViewById(android.R.id.button1);
button2 = (Button).findViewById(android.R.id.button2);
linearLayout1 = (LinearLayout).findViewById(android.R.id.firstLinear);
linearLayout2 = (LinearLayout).findViewById(android.R.id.secondLinear);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout2.setVisibility(View.VISIBLE);
linearLayout1.setVisibility(View.GONE);
}
});
}
希望對您有所幫助。
您可以在代碼中使用setVisibility()方法,如下所示:
view.setVisibility(View.GONE);
view.setVisibility(View.VISIBLE);
如果您未使用Fragment
進行任務,則可以簡單地使用setVisibility(View.VISIBLE)
和setVisibility(View.INVISIBLE)
/ setVisibility(View.GONE)
(如果其他UI元素不依賴於視圖,則將其設置為GONE)
在按鈕的onClick
上。 它看起來像這樣:
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.INVISIBLE);
}
});
您可以在一個活動中組合多個片段,也可以在多個活動中重用一個片段。 您可以將片段視為活動的模塊化部分,它具有自己的生命周期,接收自己的輸入事件,並且可以在活動運行時添加或刪除它。
使用Fragment的方法如下:
首先使用2個按鈕和一個Framelayout創建一個布局文件,稍后我們將編寫代碼以用片段中的內容替換framelayout。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin">
<LinearLayout
android:layout_width="match_parent"
android:id="@+id/ll1"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
android:id="@+id/bt1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 2"
android:id="@+id/bt2"
android:layout_alignBottom="@+id/frame"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
</LinearLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ll1"
android:id="@+id/frame"/>
</RelativeLayout>
接下來創建2個片段,例如FragmentA和fragmentB
public class fragmentA extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
創建第二個片段B
public class fragmentB extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.l1, container, false); //create a layout here
// add some code to set some text for eg
return v;
}
}
現在添加帶有2個按鈕的活動代碼,單擊按鈕1時,調用fragmentManager.beginTransaction().replace(R.id.frame, new fragmentA()).commit();
,這將用fragmentA的內容替換framelayout,類似地,當單擊按鈕2時,調用fragmentManager.beginTransaction().replace(R.id.frame, new fragmentB()).commit();
完整的代碼:
public class MyFragment extends ActionBarActivity implements View.OnClickListener {
Button bt1,bt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tt);
bt1=(Button)findViewById(R.id.bt1);
bt2=(Button)findViewById(R.id.bt2);
bt1.setOnClickListener(this);
bt2.setOnClickListener(this);
}
@Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (v.getId()) {
case R.id.bt1:
fragmentManager.beginTransaction()
.replace(R.id.frame, new BlockCallers()).commit();
break;
case R.id.bt2:
fragmentManager.beginTransaction()
.replace(R.id.frame, new smsSetting()).commit();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.