在这里搜索后,谷歌搜索了1个半小时没有解决方案,我会问这里。 (阅读我能找到的所有SO帖子,并尝试了解决方案,但他们没有工作或让我知道出了什么问题)
我得到一个空的LinearLayout,然后是另一个xml文件中的RelativeLayout。我想要的是为我正在添加的每个“设置”充气RelativeLayout。然而,当我尝试这样做时,只有第一次通货膨胀起作用,其余的不会出现。除了我怀疑它与ID问题之外,我无法确定会出现什么问题。
我尝试更改每次膨胀和添加布局时的ID,但这没有用。虽然调试可以确认mainlayout得到作为孩子的relativelayouts,并且他们的id不同。但仍然没有运气。
希望我能帮助推动正确的方向,以了解这里究竟出了什么问题。 :)
活动代码:( BuildInekBarView()膨胀并填充Relativelayout
public class SettingsActivity extends AppCompatActivity {
private SettingsDao settingsDao;
private List<Settings> allSettings;
private int lastChildId = 9000;
private int lastSeekbarId = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
//get data from database.
DaoSession daoSession = ((App) getApplication()).getDaoSession();
settingsDao = daoSession.getSettingsDao();
//get data
allSettings = settingsDao.queryBuilder().orderAsc(SettingsDao.Properties.Key).list();
//change list to hashmap
Map<String, Settings> map = new HashMap<String, Settings>();
for(Settings item : allSettings) map.put(item.getKey(), item);
//build all the views
//Hardcode for different types. Need to edit code to support new types of sensors anyway
/** Temperature **/
int tempMax = Integer.parseInt(map.get(SETTING_TEMP_MAX).getValue());
buildSeekBarView(tempMax, map.get(SETTING_TEMP_UPPER_BORDER), 0.2, getString(R.string.Settings_Temp_High_Zone_Border));
buildSeekBarView(tempMax, map.get(SETTING_TEMP_LOWER_BORDER), 0.2, getString(R.string.Settings_Temp_Low_Zone_Border));
/** Humidity **/
int humiMax = Integer.parseInt(map.get(SETTING_HUMI_MAX).getValue());
buildSeekBarView(humiMax, map.get(SETTING_HUMI_UPPER_BORDER), 1.0, getString(R.string.Settings_Humi_High_Zone_Border));
buildSeekBarView(humiMax, map.get(SETTING_HUMI_LOWER_BORDER), 1.0, getString(R.string.Settings_Humi_Low_Zone_Border));
/** Air Quality **/
int airQMax = Integer.parseInt(map.get(SETTING_AIRQ_MAX).getValue());
buildSeekBarView(airQMax, map.get(SETTING_AIRQ_UPPER_BORDER), 10.0, getString(R.string.Settings_AirQ_High_Zone_Border));
buildSeekBarView(airQMax, map.get(SETTING_AIRQ_LOWER_BORDER), 10.0, getString(R.string.Settings_AirQ_Low_Zone_Border));
//TODO at end add save btn and listener for that
}
public void buildSeekBarView(int max, Settings setting, final Double step_size, String title) {
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.linearContainer);
//inflate xml layout and insert into main layout
RelativeLayout settingView = (RelativeLayout) getLayoutInflater().inflate(R.layout.item_settings_layout, null);
if (lastSeekbarId != 0) {
//addrule
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rlp.addRule(RelativeLayout.BELOW, lastSeekbarId);
settingView.setLayoutParams(rlp);
}
settingView.setId(lastSeekbarId + 5);
lastSeekbarId = settingView.getId();
//Get Childs in created view
SeekBar seekbar = (SeekBar) settingView.findViewById(R.id.settingSlider);
TextView titleView = (TextView) settingView.findViewById(R.id.settingName);
final TextView seekBarValueView = (TextView) settingView.findViewById(R.id.seekbarValueText);
titleView.setText(title);
titleView.setTag(setting.getId()); //set tag = database ID for this setting
double proportional = (1 / step_size); //Looses a bit in the high-end as it rounds down if that is the closest int. consider adding fixed amount more to pad the possible loss
seekbar.setMax((int) ((int) max*proportional)); //multiply with proportional for resolution
seekbar.setProgress(Integer.parseInt(setting.getValue()));
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser) {
if (progress >= 0 && progress <= seekBar.getMax()) {
//Multiply with x to get away from ints and be able to set border in x intervals
String progressString = String.valueOf(progress * step_size);
seekBarValueView.setText(progressString); // the TextView Reference
seekBar.setSecondaryProgress(progress);
}
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mainLayout.addView(settingView);
}
}
RelativeLayout xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp"
android:id="@+id/item_setting_layout"
android:orientation="horizontal">
<SeekBar
android:id="@+id/settingSlider"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginEnd="70dp"
android:layout_marginTop="10dp"
android:layout_marginStart="12dp"
android:progress="0"
android:layout_below="@+id/settingName"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/settingName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="20sp"
android:layout_marginTop="18dp"
android:layout_marginStart="12dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:id="@+id/seekbarValueText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="24sp"
android:layout_alignTop="@+id/settingSlider"
android:layout_alignParentEnd="true"
android:layout_marginEnd="11dp" />
</RelativeLayout>
LinearLayout 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"
android:gravity="center_horizontal"
android:id="@+id/linearContainer"
android:orientation="horizontal">
</LinearLayout>
运行时活动的外观: