Android - 匹配父级高度,调整宽度以在水平滚动视图中保持宽高比?

时间:2015-10-12 15:59:51

标签: android width aspect-ratio

我有一个水平滚动视图,其中包含一个ImageView,后跟一个水平的LinearLayout(带有子视图)。当我从API接收ImageView时,我希望它能够在高度方面匹配父级,然后在宽度方面进行扩展以保持宽高比。我应该如何配置ImageView来实现这一目标?

谢谢!

2 个答案:

答案 0 :(得分:0)

您是否尝试过指定match_parent表示高度,wrap_content表示宽度?父级具有相同的值。

答案 1 :(得分:0)

将ImageView的高度设置为> dput(data) structure(list(cohort_year = c(2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2000L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2001L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2002L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2003L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2004L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2005L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2006L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2007L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2008L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2009L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L), program_nm = structure(c(8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), .Label = c("BS Child", "BS Criminal Justice", "BS Health Studies", "BS Human Services", "BS Nursing", "BS Psychology", "BS Public Health", "BSBA", "other" ), class = "factor"), Term_Type = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1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L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), cohort_size_yr = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 110L, 363L, 363L, 363L, 363L, 363L, 363L, 363L, 363L, 363L, 738L, 738L, 738L, 738L, 738L, 738L, 738L, 738L, 738L, 602L, 602L, 602L, 602L, 602L, 602L, 602L, 602L, 602L, 606L, 606L, 606L, 606L, 606L, 606L, 606L, 606L, 606L, 793L, 793L, 793L, 793L, 793L, 793L, 793L, 793L, 793L, 1047L, 1047L, 1047L, 1047L, 1047L, 1047L, 1047L, 1047L, 1047L, 1542L, 1542L, 1542L, 1542L, 1542L, 1542L, 1542L, 1542L, 1542L, 999L, 999L, 999L, 999L, 999L, 999L, 999L, 999L, 999L, 977L, 977L, 977L, 977L, 977L, 977L, 977L, 977L, 977L, 756L, 756L, 756L, 756L, 756L, 756L, 756L, 756L, 756L, 968L, 968L, 968L, 968L, 968L, 968L, 968L, 968L, 968L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L, 175L), grad_rate_yr_num = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), grad_rate_yr_val = c(0.338054164461162, 0.387455851221823, 0.424682338644358, 0.427085491165484, 0.43164094786971, 0.419998451137316, 0.404690563376752, 0.39455471043309, 0.471121387897879, 0.308925838858349, 0.35832752561901, 0.395554013041545, 0.397957165562672, 0.402512622266898, 0.390870125534503, 0.37556223777394, 0.365426384830278, 0.441993062295066, 0.279675200277845, 0.329076887038507, 0.366303374461042, 0.368706526982168, 0.373261983686394, 0.361619486954, 0.346311599193436, 0.336175746249774, 0.412742423714563, 0.247336159010819, 0.29673784577148, 0.333964333194016, 0.336367485715142, 0.340922942419368, 0.329280445686973, 0.31397255792641, 0.303836704982748, 0.380403382447537, 0.210471537569466, 0.259873224330127, 0.297099711752662, 0.299502864273789, 0.304058320978014, 0.29241582424562, 0.277107936485056, 0.266972083541394, 0.343538761006183, 0.169876370308747, 0.219278057069408, 0.256504544491943, 0.25890769701307, 0.263463153717295, 0.251820656984901, 0.236512769224337, 0.226376916280676, 0.302943593745464, 0.144906685947195, 0.194308372707856, 0.231534860130391, 0.233938012651518, 0.238493469355743, 0.226850972623349, 0.211543084862785, 0.201407231919124, 0.277973909383912, 0.115656047366698, 0.165057734127359, 0.202284221549894, 0.204687374071021, 0.209242830775247, 0.197600334042852, 0.182292446282289, 0.172156593338627, 0.248723270803415, 0.0808095900571362, 0.130211276817797, 0.167437764240332, 0.169840916761459, 0.174396373465685, 0.16275387673329, 0.147445988972727, 0.137310136029065, 0.213876813493853, 0.0439143903713685, 0.0933160771320296, 0.130542564554565, 0.132945717075691, 0.137501173779917, 0.125858677047523, 0.110550789286959, 0.100414936343297, 0.176981613808086, -0.000350166219887441, 0.0490515205407736, 0.0862780079633087, 0.0886811604844353, 0.0932366171886609, 0.0815941204562667, 0.066286232695703, 0.0561503797520412, 0.13271705721683, -0.0128745051020512, 0.0365271816586099, 0.073753669081145, 0.0761568216022716, 0.0807122783064972, 0.0690697815741029, 0.0537618938135392, 0.0436260408698775, 0.120192718334666, -0.0413301093276067, 0.00807157743305436, 0.0452980648555894, 0.047701217376716, 0.0522566740809416, 0.0406141773485474, 0.0253062895879837, 0.0151704366443219, 0.0917371141091104, -0.0637006429133595, -0.0142989561526984, 0.0229275312698367, 0.0253306837909633, 0.0298861404951888, 0.0182436437627946, 0.0029357560022309, -0.00720009694143085, 0.0693665805233576, -0.0993115563334487, -0.0499098695727876, -0.0126833821502525, -0.0102802296291259, -0.00572477292490037, -0.0173672696572946, -0.0326751574178583, -0.0428110103615201, 0.0337556671032684, -0.104191334110341, -0.0547896473496804, -0.0175631599271453, -0.0151600074060187, -0.0106045507017931, -0.0222470474341873, -0.0375549351947511, -0.0476907881384128, 0.0288758893263757), grad_sum_yr_num = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9", "1", "2", "3", "4", "5", "6", "7", "8", "9"), grad_sum_yr_val = c(0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 3, 4, 4, 4, 4, 4, 0, 19, 43, 47, 51, 53, 53, 54, 54, 7, 46, 110, 129, 143, 147, 147, 96.9108663255262, 150, 8, 79, 199, 269, 285, 290, 295, 281, 300, 5, 62, 175, 230, 245, 249, 218, 219, 254, 6, 58, 167, 212, 228, 231, 229, 109, 235, 0, 35, 106, 158, 180, 189, 86, 0, 190, 1, 25, 91, 148, 183, 89, 0, 0, 193, 2, 30, 112, 162, 79, 0, 0, 0, 187, 3, 40, 108, 67, 0, 0, 0, 0, 135, 6, 40, 42, 0, 0, 0, 0, 0, 78, 3, 16, 0, 0, 0, 0, 0, 0, 21, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0)), .Names = c("cohort_year", "program_nm", "Term_Type", "Degree_Level", "certificate_flag", "cohort_size_yr", "grad_rate_yr_num", "grad_rate_yr_val", "grad_sum_yr_num", "grad_sum_yr_val" ), row.names = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "30", "32", "34", "36", "38", "40", "42", "44", "46", "48", "50", "52", "54", "56", "58", "60", "62", "64", "66", "68", "70", "72", "74", "76", "78", "80", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "103", "108", "113", "118", "123", "128", "133", "138", "143", "155", "166", "177", "188", "199", "210", "221", "232", "243", "258", "273", "288", "303", "318", "333", "348", "363", "378", "396", "414", "432", "450", "468", "486", "504", "522", "540", "559", "578", "597", "616", "635", "654", "673", "692", "711", "731", "751", "771", "791", "811", "831", "851", "871", "891", "911", "932", "953", "974", "995", "1016", "1037", "1058", "1079", "1099", "1119", "1139", "1159", "1179", "1199", "1219", "1239", "1259" ), class = "data.frame") ,使其与父级的高度相匹配。然后根据您尝试实现的宽高比计算宽度,例如 - 16:9 =身高/0.5625。 - 并将宽度设置为。

修改 要获得目标高度,然后根据该值计算目标宽度,首先获取父布局的高度,然后计算宽度,然后设置ImageView的LayoutParams,然后添加视图。像这样......

match_parent