我是android开发的初学者。我正在尝试制作一个支持横向和纵向图像的布局。
例如,如果图像是肖像,那么它周围应该有白色空间,如果图像是风景,那么它将占据图像视图的整个宽度,并且它们都应保持纵横比。我使用json来获取图像从URL和使用数组适配器来扩展listview中的布局
当布局膨胀时,线性布局的重量会自动改变
我不知道为什么会这样做?应该采取什么方法呢?我使用java缩放图像还是有其他方法?
以下是布局的屏幕截图 First row of the listview Second row pf the listview
注意:您可以看到,在第二个屏幕截图中,第一个线性布局的高度大于第一个屏幕截图中的线性布局(显示用户的个人资料图片)
timelinerowlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:padding="10dp">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.35"
android:background="@drawable/border">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp">
<com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/userProfilePicture"
android:layout_width="40dp"
android:layout_height="match_parent"
android:src="@drawable/ppplaceholder"
app:riv_border_color="#333333"
app:riv_border_width="2dip"
app:riv_corner_radius="5dip"
app:riv_mutate_background="true"
app:riv_oval="true"
app:riv_tile_mode="repeat" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@+id/userProfilePicture"
android:layout_toRightOf="@+id/userProfilePicture"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5">
<ImageView
android:id="@+id/userpostimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/bc" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.16">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.gitesh.timeline.MainActivity">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.v4.widget.SwipeRefreshLayout>
MainActivity.java
package com.example.gitesh.timeline;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.Toast;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.TextHttpResponseHandler;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Classes.PostAdapter;
import Classes.posts;
import cz.msebera.android.httpclient.Header;
public class MainActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {
ListView datalist;
PostAdapter PostAdapter;
private SwipeRefreshLayout swipeRefreshLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
datalist = (ListView) findViewById(R.id.listview);
datalist.setOnScrollListener(new AbsListView.OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (datalist.getChildAt(0) != null) {
swipeRefreshLayout.setEnabled(datalist.getFirstVisiblePosition() == 0 && datalist.getChildAt(0).getTop() == 0);
}
}
});
swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_refresh_layout);
Typeface cFont = Typeface.createFromAsset(getAssets(), "SAMARN__.TTF");
Typeface fontAwesome = Typeface.createFromAsset(getAssets(), "fontawesome-webfont.ttf");
PostAdapter = new PostAdapter(this, R.layout.timelinerowlayout, cFont);
datalist.setAdapter(PostAdapter);
swipeRefreshLayout.setOnRefreshListener(this);
/**
* Showing Swipe Refresh animation on activity create
* As animation won't start on onCreate, post runnable is used
*/
swipeRefreshLayout.post(new Runnable() {
@Override
public void run() {
swipeRefreshLayout.setRefreshing(true);
bindData();
}
}
);
}
/**
* This method is called when swipe refresh is pulled down
*/
@Override
public void onRefresh() {
bindData();
}
public void bindData() {
try {
AsyncHttpClient client = new AsyncHttpClient(true, 80, 443);
client.get("http://indianroute.roms4all.com/fetch_post.php", new TextHttpResponseHandler() {
@Override
public void onSuccess(int statusCode, Header[] headers, String res) {
decodeJson(res);
}
@Override
public void onFailure(int statusCode, Header[] headers, String res, Throwable t) {
// called when response HTTP status is "4XX" (eg. 401, 403, 404)
Toast.makeText(MainActivity.this, "" + res, Toast.LENGTH_SHORT).show();
}
}
);
} catch (Exception e) {
Toast.makeText(MainActivity.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void decodeJson(String result) {
try {
swipeRefreshLayout.setRefreshing(true);
JSONArray jArr = new JSONArray(result);
String username, story, picture, check_in, userprofilePicture;
for (int count = 0; count < jArr.length(); count++) {
JSONObject obj = jArr.getJSONObject(count);
username = obj.getString("username");
story = obj.getString("story");
picture = obj.getString("picture");
check_in = obj.getString("check_in");
userprofilePicture = obj.getString("userprofilePicture");
posts posts = new posts(username, story, picture, check_in, userprofilePicture);
swipeRefreshLayout.setRefreshing(false);
PostAdapter.add(posts);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
PostAdapter.java
package Classes;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.gitesh.timeline.R;
import com.makeramen.roundedimageview.RoundedImageView;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Gitesh on 14-06-2016.
*/
public class PostAdapter extends ArrayAdapter {
List list = new ArrayList();
Typeface samarn;
public PostAdapter(Context context, int resource, Typeface cFont) {
super(context, resource);
samarn = cFont;
}
@Override
public void add(Object object) {
super.add(object);
list.add(object);
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row;
row = convertView;
PostHolder postHolder;
if (row == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = layoutInflater.inflate(R.layout.timelinerowlayout, parent, false);
postHolder = new PostHolder();
postHolder.username = (TextView) row.findViewById(R.id.username);
postHolder.userPostImage = (ImageView) row.findViewById(R.id.userpostimage);
postHolder.userprofilePicture = (RoundedImageView) row.findViewById(R.id.userProfilePicture);
row.setTag(postHolder);
} else {
postHolder = (PostHolder) row.getTag();
}
posts posts = (posts) this.getItem(position);
postHolder.username.setTypeface(samarn);
postHolder.username.setText(posts.getUsername());
Picasso.with(getContext()).load(posts.getUserProfilePicture()).placeholder(R.drawable.ppplaceholder).priority(Picasso.Priority.HIGH).into(postHolder.userprofilePicture);
Picasso.with(getContext()).load(posts.getPictue()).placeholder(R.drawable.postplaceholder).into(postHolder.userPostImage);
return row;
}
static class PostHolder {
TextView username;
ImageView userPostImage;
com.makeramen.roundedimageview.RoundedImageView userprofilePicture;
}
}
答案 0 :(得分:0)
如果您使用水平线性布局(android:orientation="horizontal"
),则将宽度设为0dp
,如果是垂直线性布局,则将高度设为0dp
然后给孩子的观点加权。
答案 1 :(得分:0)
您已定义weight_sum = 3
,该布局的内部视图相应地具有权重0.35
,1.5
和1.16
。
0.35 + 1.5 + 1.16 = 3.01
因此,您为内部视图定义的权重总和并不完全等于3
答案 2 :(得分:0)
尝试使用此替换您的布局,我已更改布局权重
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ccc"
android:padding="10dp">
<!-- A CardView that contains a TextView -->
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
card_view:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@drawable/border">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
<com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/userProfilePicture"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:src="@drawable/ppplaceholder"
app:riv_border_color="#333333"
app:riv_border_width="2dip"
app:riv_corner_radius="5dip"
app:riv_mutate_background="true"
app:riv_oval="true"
app:riv_tile_mode="repeat" />
<TextView
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toEndOf="@+id/userProfilePicture"
android:layout_toRightOf="@+id/userProfilePicture"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.3">
<ImageView
android:id="@+id/userpostimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:src="@drawable/bc" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>