Android自定义表格布局
我想创建如下的tablayout视图: 以下是我为选项卡布局编写的代码:Android自定义表格布局,android,android-layout,layout,android-tablayout,Android,Android Layout,Layout,Android Tablayout,我想创建如下的tablayout视图: 以下是我为选项卡布局编写的代码: public class MainActivity extends AppCompatActivity { TabLayout tabLayout; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setConte
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitle("Suman Restaurant");
tabLayout = findViewById(R.id.tabLayout);
tabLayout.addTab(tabLayout.newTab().setText("Chinese"));
tabLayout.addTab(tabLayout.newTab().setText("Noodles"));
tabLayout.addTab(tabLayout.newTab().setText("Snacks"));
tabLayout.addTab(tabLayout.newTab().setText("Pizza"));
}
}
我怎样才能像附件中的图片一样设计选项卡布局?他们的答案并不简单;但是你可以在线性布局中这样做,你所要做的就是添加一个相对布局,然后让最左边的按钮垂直居中,然后向左,然后每个其他按钮都是相对的,这个按钮。稍微向右。希望这会对你有所帮助,记得在提问之前做一些研究。我知道现在发布这个答案已经太迟了,但我觉得这对将来的人会有帮助,所以我将发布它。
我在做类似的选项卡布局,我搜索了很多,但没有找到任何有用的解决方案。这里是位长度的解决方案,但它将工作,你寻找 添加一个方法,该方法将在选项卡布局中添加选项卡,如
private void addTabsOnTablayout() {
tablayout.addTab(tablayout.newTab().setText("Chinese"), 0);
tablayout.addTab(tablayout.newTab().setText("Noodles"), 1);
tablayout.addTab(tablayout.newTab().setText("Snacks"), 2);
tablayout.addTab(tablayout.newTab().setText("Pizza"), 3);
tablayout.addOnTabSelectedListener(this);
for (int i = 0; i < tablayout.getTabCount(); i++) {
TabLayout.Tab tab = tablayout.getTabAt(i);
if (tab != null) {
TextView tabTextView = new TextView(getActivity());
tabTextView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
tabTextView.setTextSize(16);
tabTextView.setPadding(0, 4, 0, 4);
tabTextView.setBackgroundResource(R.drawable.tab_inactive);
tabTextView.setTextColor(getResources().getColor(R.color.tab_unselected_text_color));
tab.setCustomView(tabTextView);
tabTextView.setText(tab.getText());
// First tab is the selected tab, so if i==0 then set BOLD typeface
if (i == 0) {
tabTextView.setBackgroundResource(R.drawable.tab_active);
tabTextView.setTextColor(getResources().getColor(R.color.tab_selected_text_color));
tab.setCustomView(tabTextView);
}
}
}
}
最后为tab_active和tab_inactive创建一个可绘制的。请使用您尝试的代码更新您的问题,以便社区可以帮助您改进您的想法。请添加一些代码,然后我们可以发现问题并解决您的问题!我会很快更新这个问题,我是新来的,所以有点困惑。谢谢苏拉杰·加吉发布答案。我跳过了tabLayout部分,开始处理剩下的东西,现在我在代码中添加了这个,它工作起来很有魅力。
@Override
public void onTabSelected(TabLayout.Tab tab) {
TextView tabTextView = (TextView) tab.getCustomView();
tabTextView.setBackgroundResource(R.drawable.tab_active);
tabTextView.setTextColor(getResources().getColor(R.color.tab_selected_text_color));
tab.setCustomView(tabTextView);
showSnack();
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
TextView tabTextView = (TextView) tab.getCustomView();
tabTextView.setBackgroundResource(R.drawable.tab_inactive);
tabTextView.setTextColor(getResources().getColor(R.color.tab_unselected_text_color));
tab.setCustomView(tabTextView);
}