浏览代码

Start creating buttons dynamically

pull/1/head
Malte Schmitz 8 年前
父节点
当前提交
e06d58d0d7
共有 3 个文件被更改,包括 55 次插入58 次删除
  1. +29
    -0
      app/src/main/java/de/mlte/soundboard/MainActivity.kt
  2. +1
    -58
      app/src/main/res/layout/activity_main.xml
  3. +25
    -0
      app/src/main/res/layout/layout_button.xml

+ 29
- 0
app/src/main/java/de/mlte/soundboard/MainActivity.kt 查看文件

@@ -8,7 +8,10 @@ import android.media.MediaPlayer
import android.net.Uri
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.LayoutInflater
import android.view.animation.LinearInterpolator
import android.widget.FrameLayout
import android.widget.GridLayout
import android.widget.ProgressBar
import android.widget.TextView
import java.io.BufferedInputStream
@@ -20,6 +23,8 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

duplicateButton()

loadPreferences()

var player: MediaPlayer? = null
@@ -75,6 +80,30 @@ class MainActivity : AppCompatActivity() {
}
}

private fun duplicateButton() {
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val parent = findViewById<GridLayout>(R.id.grid_layout)

parent.columnCount = 2

for (row in 0..1) {
for (col in 0..1) {
val newFrame = inflater.inflate(R.layout.layout_button, null)
// newFrame.findViewById<ProgressBar>(R.id.progress_bar)
// newFrame.findViewById<TextView>(R.id.text_view_button)

val params = GridLayout.LayoutParams()
params.columnSpec = GridLayout.spec(col, 1, 1.0f)
params.rowSpec = GridLayout.spec(row, 1, 1.0f)
params.width = 0
params.height = 0
newFrame.layoutParams = params

parent.addView(newFrame)
}
}
}

private fun loadPreferences() {
val preferences = getPreferences(Context.MODE_PRIVATE)
val caption = preferences.getString("caption", "")


+ 1
- 58
app/src/main/res/layout/activity_main.xml 查看文件

@@ -7,69 +7,12 @@
tools:context="de.mlte.soundboard.MainActivity">

<GridLayout
android:id="@+id/grid_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="1"
android:padding="1dp">

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="0"
android:layout_rowWeight="1"
android:padding="2dp">

<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="false"
android:progressDrawable="@drawable/progress_bar_states" />

<TextView
android:id="@+id/text_view_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="6dp"
android:text="Enter"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />

</FrameLayout>

<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_columnWeight="1"
android:layout_row="1"
android:layout_rowWeight="1"
android:padding="2dp">

<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="false"
android:progressDrawable="@drawable/progress_bar_states" />

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="6dp"
android:text="Enter"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />

</FrameLayout>

</GridLayout>

</android.support.constraint.ConstraintLayout>

+ 25
- 0
app/src/main/res/layout/layout_button.xml 查看文件

@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="2dp">
<ProgressBar
android:id="@+id/progress_bar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:indeterminate="false"
android:progressDrawable="@drawable/progress_bar_states" />

<TextView
android:id="@+id/text_view_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:padding="6dp"
android:text="Enter"
android:textColor="@android:color/white"
android:textSize="16sp"
android:textStyle="bold" />

</FrameLayout>

正在加载...
取消
保存