多個圓形button整齊排列,求教

weixin_43684035 2020-05-16 10:10:05
想做個界面,要做圖形的button,每行四個,排7行,平均排列每行每列,如圖1,但現在圓行button會被拉伸,如圖2
求大神指點。在任何大小界面下都可以是圓形button,而且平均排列,間距一樣
附上代碼:


主代碼:
public class MainActivity extends AppCompatActivity {

private static String[] irButtonName ={
"CH1","CH2","ON","LOCK","10","50","OFF","AUTO","100","300","SENS+","TEST","500",
"1000","SENS","SemiAuto","2000","24H","EYE","PULSE","30S","1m","3m","5m",
"10m","20m","30m","60m"
};
private LinearLayout contentView; //声明一个布局文件的引用
private View.OnClickListener btnOnClick; //声明button监听的引用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main);
contentView = (LinearLayout) findViewById(R.id.contentView);
this.initView(contentView);
}
public void initView(LinearLayout parentLL){
// LinearLayout parentLL = (LinearLayout) contentView;
int size = irButtonName.length;
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT); //每行的水平LinearLayout 的参数

layoutParams.setMargins(10,23,10,23);
ArrayList<Button> childBtns = new ArrayList<Button>();
int totoalBtns=0; //添加到第几个 button了
int yy=0;
for (int i=0;i<size;i++){
String item = irButtonName[i];
LinearLayout.LayoutParams itemParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT); //button的布局参数
int length = item.length();
itemParams.weight=3;
itemParams.width=0;
totoalBtns++;
yy++;
System.out.println("AddButton :"+"add =--------- "+yy);
// itemParams.width = 0;
itemParams.setMargins(55,5,55,5);
Button childBtn =(Button) LayoutInflater.from(this).inflate(R.layout.button,null);
//创建Button
// childBtn.setText(item);
// childBtn.setTag(item);
childBtn.setLayoutParams(itemParams); // 设定button的布局参数
childBtns.add(childBtn); //Add List
if(totoalBtns>=4){
totoalBtns=0;

LinearLayout horizLL = new LinearLayout(this);
horizLL.setOrientation(LinearLayout.HORIZONTAL);
horizLL.setLayoutParams(layoutParams);
for (Button addBtn:childBtns)
{
// Log.d("AddButton","add"+"="+addBtn.getText());
System.out.println("AddButton :"+"add = "+addBtn.getText());
horizLL.addView(addBtn);
}
parentLL.addView(horizLL);
childBtns.clear();
}
}
if(!childBtns.isEmpty()){
LinearLayout horizLL = new LinearLayout(this);
horizLL.setLayoutParams(layoutParams);
horizLL.setOrientation(LinearLayout.HORIZONTAL);
for (Button addBtn:childBtns){
horizLL.addView(addBtn);
}
parentLL.addView(horizLL);
childBtns.clear();
}
}
}

button.xm代碼如下: test.png=36*36

<?xml version="1.0" encoding="utf-8"?>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:singleLine="true"
android:ellipsize="end"
android:background="@drawable/test"
android:textSize="18dp"
>

</Button>

content_main.xml代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical">

</LinearLayout>
...全文
991 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
云农民工 2020-09-08
  • 打赏
  • 举报
回复
为何不直接使用Recyclerview呢,简单方便
JMSissi 2020-09-08
  • 打赏
  • 举报
回复
画一个圆用作背景,然后用GridView或者Recyclerview都可以
weixin_43684035 2020-05-22
  • 打赏
  • 举报
回复
引用 1 楼 只为搞笑 的回复:
你得动态算宽度,然后让每个button的高度=宽度


高度=寬度,一語點醒,十分感謝
只为搞笑 2020-05-18
  • 打赏
  • 举报
回复
你得动态算宽度,然后让每个button的高度=宽度

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧