我正在尝试使用四个动态生成的按钮来占用GridLayout中的偶数空间。我已经尝试了所有可能的东西,从设置LayoutParams
到获得onMeasuredWidth
,但我简单无法实现我想要的。
This是我想要的,以下是我最终得到的
我调查了this和其他许多人,他们都没有提出解决方案。
以下是包含GridLaayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="accordiontry.juspay.accordiontry.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2">
</GridLayout>
</LinearLayout>
以下是 MainActivity.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private GridLayout gridLayout;
private final int NumOfCol = 2;
private final int NumOfRows = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
init();
generateView();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void init()
{
gridLayout = (GridLayout) findViewById(R.id.gridLayout);
gridLayout.setBackgroundColor(Color.BLUE);
gridLayout.measure(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
public void generateView()
{
Button btn1 = new Button(this);
Button btn2 = new Button(this);
btn1.setText("BUTTON 1");
btn1.setWidth(gridLayout.getMeasuredWidth());
gridLayout.addView(btn1);
btn2.setText("BUTTON 2");
btn2.setWidth(gridLayout.getMeasuredWidth());
gridLayout.addView(btn2);
}
}
答案 0 :(得分:0)
我已检查过您的代码,请按照xml布局
进行操作 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="accordiontry.juspay.accordiontry.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical">
<GridLayout
android:id="@+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:columnCount="2">
</GridLayout>
</LinearLayout>
或设置修复高度gridlayout
这在我的案例中运作良好
答案 1 :(得分:0)
“动态”是什么意思?用户添加按钮?如果是这样,您可以添加已知宽度和高度的按钮并将它们放在您想要使用的位置android:alpha="0"
(或1,抱歉,不记得,同时尝试两者)和android:enabled="false"
并删除按钮中的所有信息。这将需要很长时间,但你会完全知道他们会在他们的位置。