I have created an app in which I am fetching data from server. So, I have used JSON parsing in my code. I am successfully parsed my JSON from server. Now I want to set buttons in layout according to their visibility getting from JSON response.
That is if visibility true then button should visible and if false button should disable. It works but if some button has visibility false then there is a space in layout. So how to set in layout dynamically that all visible buttons show in line.
I have created three linear layouts each contains three buttons.Now if json response says visibility false for some button I set visibility to off for that button.
if (Menuname.equals("StudentDailyDiary")) {
studentdiary.setVisibility(View.VISIBLE);
moreOptionLayout.setVisibility(View.VISIBLE);
hrview.setVisibility(View.VISIBLE);
rlleftview.setVisibility(View.VISIBLE);
rlrightview.setVisibility(View.VISIBLE);
}
if (Menuname.equals("BookSearch")) {
studlibrary.setVisibility(View.VISIBLE);
moreOptionLayout.setVisibility(View.VISIBLE);
hrview.setVisibility(View.VISIBLE);
rlleftview.setVisibility(View.VISIBLE);
rlrightview.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)
Say in first layout three buttons are there from which second button has visibility false then on position of second button third button should be placed there should be no blank space for buttons in all layout.That is if third button has visibility false then first button of second layout should set on position of third button.

我认为要使用的正确ViewGroup是GridLayout。
这里有个简单的例子:
activity_main.xml
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gridView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="80dp"
android:gravity="center"
android:numColumns="3"
android:stretchMode="columnWidth" />
Run Code Online (Sandbox Code Playgroud)
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val numbers = arrayOf("BUTTON1", "BUTTON2", "BUTTON4", "BUTTON5", "BUTTON6")
val adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, numbers)
gridView.adapter = adapter
gridView.onItemClickListener = AdapterView.OnItemClickListener {
parent, v, position, id ->
Toast.makeText(applicationContext, (v as TextView).text, Toast.LENGTH_SHORT).show()
}
}
}
Run Code Online (Sandbox Code Playgroud)
本示例使用String,但是您可以根据需要创建一个Adapter。现在,在执行JSON响应后,只需填充数组并将其设置为适配器即可。
希望能帮助到你
| 归档时间: |
|
| 查看次数: |
97 次 |
| 最近记录: |