Java 在android中如何在标签旁边放置按钮?(与用户界面相关)
我想在我的应用程序中的标签旁边放置一个按钮。关于如何构建选项卡,我已经给出了下面的代码 这是XML } 这是我运行应用程序时的当前视图 我想做的是在今天的地方放一个按钮。我该怎么做呢 我排除了一些xml代码,这些代码将显示按下的状态,因为问题将变得冗长Java 在android中如何在标签旁边放置按钮?(与用户界面相关),java,android,button,user-interface,Java,Android,Button,User Interface,我想在我的应用程序中的标签旁边放置一个按钮。关于如何构建选项卡,我已经给出了下面的代码 这是XML } 这是我运行应用程序时的当前视图 我想做的是在今天的地方放一个按钮。我该怎么做呢 我排除了一些xml代码,这些代码将显示按下的状态,因为问题将变得冗长 感谢您的输入和时间。因为TabLayout必须是布局的根节点,所以您可以尝试以下操作: 使用3个选项卡制作表格布局,在第4个选项卡处,您可以自己制作一个自定义按钮。因为表格布局必须是布局的根节点,所以您可以尝试以下操作: 使用3个选项卡制作
感谢您的输入和时间。因为
TabLayout
必须是布局的根节点,所以您可以尝试以下操作:
使用3个
选项卡制作表格布局
,在第4个选项卡
处,您可以自己制作一个自定义按钮。因为表格布局
必须是布局的根节点,所以您可以尝试以下操作:
使用3个选项卡制作表格布局
,在第4个选项卡
的位置,您可以自己制作一个自定义按钮。
我使用了下面的代码来生成这个结果。我已将第四个选项卡clickable设置为false。只有选项卡内的按钮才可单击。因此,这会给人一种印象,它不是一个标签。希望这会有所帮助
活动:
public class MainActivity extends TabActivity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
setupTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.item_seperator);
setupTab(new TextView(this), "Month");
setupTab(new TextView(this), "Week");
setupTab(new TextView(this), "Day");
final View view = new Button(this);
View tabview = LayoutInflater.from(this).inflate(
R.layout.button_tabs_bg, null);
Button button = (Button) tabview.findViewById(R.id.tabsButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button click",
Toast.LENGTH_SHORT).show();
}
});
TabSpec setContent = mTabHost.newTabSpec("").setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
mTabHost.getTabWidget().getChildTabViewAt(3).setEnabled(false);
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
tab\u bg\u selector.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
tabs_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
按钮选项卡bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
#333333
#999999
#999966
我使用了下面的代码来生成这个结果。我已将第四个选项卡clickable设置为false。只有选项卡内的按钮才可单击。因此,这会给人一种印象,它不是一个标签。希望这会有所帮助
活动:
public class MainActivity extends TabActivity {
private TabHost mTabHost;
private void setupTabHost() {
mTabHost = (TabHost) findViewById(android.R.id.tabhost);
mTabHost.setup();
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
setupTabHost();
mTabHost.getTabWidget().setDividerDrawable(R.drawable.item_seperator);
setupTab(new TextView(this), "Month");
setupTab(new TextView(this), "Week");
setupTab(new TextView(this), "Day");
final View view = new Button(this);
View tabview = LayoutInflater.from(this).inflate(
R.layout.button_tabs_bg, null);
Button button = (Button) tabview.findViewById(R.id.tabsButton);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Button click",
Toast.LENGTH_SHORT).show();
}
});
TabSpec setContent = mTabHost.newTabSpec("").setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
mTabHost.getTabWidget().getChildTabViewAt(3).setEnabled(false);
}
private void setupTab(final View view, final String tag) {
View tabview = createTabView(mTabHost.getContext(), tag);
TabSpec setContent = mTabHost.newTabSpec(tag).setIndicator(tabview)
.setContent(new TabContentFactory() {
public View createTabContent(String tag) {
return view;
}
});
mTabHost.addTab(setContent);
}
private static View createTabView(final Context context, final String text) {
View view = LayoutInflater.from(context)
.inflate(R.layout.tabs_bg, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(text);
return view;
}
}
tab\u bg\u selector.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
tabs_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
按钮选项卡bg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tabsLayout" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="@color/buttontab"
android:padding="10dip" android:gravity="center" android:orientation="vertical">
<Button
android:id="@+id/tabsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:textColor="@android:color/black"
android:textSize="15dip" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="tabdark">#333333</color>
<color name="tablight">#999999</color>
<color name="buttontab">#999966</color>
</resources>
#333333
#999999
#999966