[英]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.